如何允许用户使用python循环输入和存储数组

2024-05-16 22:22:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我的个人项目有问题,我的python技能非常基础,但如果有任何帮助,我将不胜感激

问题: 任务1 为了模拟所需的监测,编写一个例程,允许输入婴儿的体温 摄氏度。例行程序还应检查温度是否在可接受范围内 高或过低,并在每种情况下输出适当的消息。 任务2 编写另一个例程,将三小时内的温度存储在数组中。这 例行程序应输出最高和最低温度,并计算它们之间的差异 温度。 注:更多强调任务2

我失败的尝试:

from array import array
print("BABY TEMPERATURE CHECKER")
MinBbyTemp = float(36.0)
MaxBbyTemp = float(37.5) 
routTemp = array("i", [])
BabyTemp = float(input("What is the temperature of the baby?"))
if BabyTemp < MinBbyTemp:
   print("The temperature of the baby is low/unusual and needs to be worked on")
elif BabyTemp > MaxBbyTemp:
   print("The temperature of the baby is too high and above the average")
else:
   print("The temperature inputted is out of range")

Tags: oftheisfloat温度array例程baby
3条回答

首先,我建议使用Pythons内置的列表类型,而不是数组。 其次,float(36.0)是多余的,因为Python会将36.0推断为浮点

您的代码实际上相当不错,但如果您想输入多个值,则需要一个循环。这个循环将永远持续下去,因此如果您希望它结束,您需要将while True更改为while some_boolean,并让用户更改some_booleanfor循环

max_temp = 37.5
min_temp = 36.0
recorded_temps = [] # Creates recorded_temps as list.
while True:
    baby_temp = float(input("What is the temperature of the baby?"))
    # Note: if user input cannot be converted to float, this will yield a type error.
    if max_temp < baby_temp:
       print("The temperature of the baby is too high and above the average.")
    elif baby_temp < min_temp:
        print("The temperature of the baby is low/unusual and needs to be worked on.")
    elif (min_temp < baby_temp) and (baby_temp < max_temp):
        print("The temperature is within a healthy range.")

    recorded_temps.append(baby_temp) # Adds baby_temp to recorded_temps.

执行此循环后,将有一个Python温度列表,您可以将其处理为Daniel Hao explained.

@EmeluDev-让我们试着一步一步解决这个问题。假设你已经收集了每日温度。测量已完成(任务1已完成)。然后从任务2开始:

# Task 2 Demo Template

dailyTemps = [36.9, 36.7, 37.2, 37.5, 36.8, 38.2]


highest  = max(dailyTemps)
lowest   = min(dailyTemps)
difference = highest - lowest

print(f' highest Temp: {highest}, loweset Temp: {lowest} ')
print(f' the difference is: {difference} ')

Output: 
highest Temp: 38.2, loweset Temp: 36.7 
the difference is: 1.5 

我会这样做:

import sched
import time

#PART 1

print("###BABY TEMPERATURE CHECKER###")

MinBbyTemp = float(36.0)
MaxBbyTemp = float(37.5)

array = []
temp = 0

s = sched.scheduler(time.time, time.sleep)

BabyTemp = float(input("What is the temperature of the baby: "))

if BabyTemp < MinBbyTemp:
    print("The temperature of the baby is low/unusual and needs to be worked on")
elif BabyTemp > MaxBbyTemp:
    print("The temperature of the baby is too high and above the average")
else:
    print("The temperature of the baby is normal")

#PART 2

def get_input(string):
    value = input(str(string))
    
    if len(value) > 0:
        return value
    else:
        print("You Must Enter Something! Try Again!")
        get_input(str(string))

def wait(sc):
    global temp
    global array
    
    temp = get_input("Enter The Baby\'s Temperature: ")
    array.append(temp)

def get_max(arr):
    max_ = arr[0]  
         
    for i in range(0, len(arr)):
        if arr[i] == None:
            pass
        else:
            if int(arr[i]) < int(max_):    
                max_ = arr[i]

    return max_

def get_min(arr):
    min_ = arr[0]  
         
    for i in range(0, len(arr)):
        if arr[i] == None:
            pass
        else:
            if int(arr[i]) > int(min_):
                min_ = arr[i]

    return min_
    
for i in range(0, 36):
    s.enter(300, 1, wait, (s,))
    s.run()

    print("{} / 36".format(str(i + 1)))


print(array)

min_temp = get_max(array)
max_temp = get_min(array)
range_temp = int(max_temp) - int(min_temp)

print("The Minimum Temperature Was " + str(min_temp))
print("The Maximum Temperature Was " + str(max_temp))
print("The Range Of Temperature Was " + str(range_temp))

相关问题 更多 >