Python 使用范围函数时的输入验证

Python 使用范围函数时的输入验证,python,Python,我正在创建一个函数,将输入附加到列表中。一天中需要24个项目,我正在使用range函数来实现这一点。我需要验证这个输入。然而,我似乎不能让它每次都正确地验证,或者我已经得到了正确的验证,但是代码提示超过24个输入 def get(list): for i in range(24): tem=float(input("Hourly tempature (00:00-23:00): ")) if tem < -50 or tem > 150: print

我正在创建一个函数,将输入附加到列表中。一天中需要24个项目,我正在使用range函数来实现这一点。我需要验证这个输入。然而,我似乎不能让它每次都正确地验证,或者我已经得到了正确的验证,但是代码提示超过24个输入

def get(list):
for i in range(24):
    tem=float(input("Hourly tempature (00:00-23:00): "))
    if tem < -50 or tem > 150:
        print ("Enter tempatures between -50 and 130")
    else:
        tem=float(input("Hourly tempature (00:00-23:00)"))


    list.append(tem)
def get(列表):
对于范围(24)内的i:
tem=浮动(输入(“小时温度(00:00-23:00):”)
如果tem<-50或tem>150:
打印(“输入介于-50和130之间的温度”)
其他:
tem=浮动(输入(“小时温度(00:00-23:00)”)
list.append(tem)

输入
放在
else
块中,而不是
if
,意味着当第一次输入正确而不是不正确时,代码会在循环中再次提示输入


在任何情况下,如果他们输入错误,它将不会再次检查。您需要使用while循环。请参见

输入
放在
else
块中,而不是
if
,这意味着当第一个输入正确而不是不正确时,代码会在循环中再次提示输入

在任何情况下,如果他们输入错误,它将不会再次检查。您需要使用while循环。请参见以下几点:

<> LI>你可能要考虑告诉用户他们设定温度的时间:例如:代码>输入(“温度”在+STR(i)+:“00小时:”)< /代码>
  • 您应该澄清您是希望温度小于或等于150,还是小于或等于130,因为此时提供给用户的文本表明温度必须小于或等于130,但您的if语句表明温度必须小于或等于150
  • 您可能不应该使用内置的
    列表
    作为变量。尝试使用描述其用途的变量,如
    myTemperatureList
  • 当前,当输入在温度范围内时,代码会再次提示输入,但当温度超出范围时,会打印错误消息(未获得额外输入)。这意味着,当温度输入在范围内时,将提示用户输入两次,第二次输入将添加到您的列表中。但是,当温度输入超出边界时,尽管会打印错误消息,但不会提示用户进行第二次输入,超出边界的温度将添加到列表中 展开上面的第3点,您希望输入验证代码做的是提示输入温度,并检查该温度是否在范围内。如果是,则应将该值添加到列表中,如果不是,则应丢弃该值,并应提示用户再次输入

    这可以通过几种方式实现。例如,使用
    while
    循环,一个可能的解决方案可能是:

    def get(myTemperatureList):
        for i in range(24):
            while True:
                #This is so that when an error message is printed,
                #the user is prompted again for input, for as long as they
                #are providing bad input
                try:
                    #You'll want this try-except block in case the user doesn't enter a number
                    tem=float(input("Temperature at "+str(i)+":00 hours:"))
                    #This is basically the sames as in your code
    
                    if tem < -50 or tem > 150:
    
                        print ("Enter a temperature between -50 and 130")
                        #Same logic as in your code, prints an error message
                        #when the temperature is out of bounds
                    else:
                        #If the temperature is valid, break out of the while loop
    
                        break
                except ValueError:
                    print("Enter a number")
            myTemperatureList.append(tem)
    
    def get(myTemperatureList):
    对于范围(24)内的i:
    尽管如此:
    #这样,当打印错误消息时,
    #只要用户愿意,就会再次提示用户输入
    #他们提供了错误的输入
    尝试:
    #如果用户没有输入数字,您将需要此try Exception块
    tem=浮动(输入(“温度在”+str(i)+“:00小时:”)
    #这基本上与代码中的相同
    如果tem<-50或tem>150:
    打印(“输入介于-50和130之间的温度”)
    #与代码中的逻辑相同,打印错误消息
    #当温度超出范围时
    其他:
    #如果温度有效,则断开while循环
    打破
    除值错误外:
    打印(“输入数字”)
    我的温度列表附加(tem)
    
    您还可以通过其他方式解决此问题,例如,使用带有验证函数的递归。

    以下几点:

    <> LI>你可能要考虑告诉用户他们设定温度的时间:例如:代码>输入(“温度”在+STR(i)+:“00小时:”)< /代码>
  • 您应该澄清您是希望温度小于或等于150,还是小于或等于130,因为此时提供给用户的文本表明温度必须小于或等于130,但您的if语句表明温度必须小于或等于150
  • 您可能不应该使用内置的
    列表
    作为变量。尝试使用描述其用途的变量,如
    myTemperatureList
  • 当前,当输入在温度范围内时,代码会再次提示输入,但当温度超出范围时,会打印错误消息(未获得额外输入)。这意味着,当温度输入在范围内时,将提示用户输入两次,第二次输入将添加到您的列表中。但是,当温度输入超出边界时,尽管会打印错误消息,但不会提示用户进行第二次输入,超出边界的温度将添加到列表中 展开上面的第3点,您希望输入验证代码做的是提示输入温度,并检查该温度是否在范围内。如果是,则应将该值添加到列表中,如果不是,则应丢弃该值,并应提示用户再次输入

    这可以通过几种方式实现。例如,使用
    while
    循环,一个可能的解决方案可能是:

    def get(myTemperatureList):
        for i in range(24):
            while True:
                #This is so that when an error message is printed,
                #the user is prompted again for input, for as long as they
                #are providing bad input
                try:
                    #You'll want this try-except block in case the user doesn't enter a number
                    tem=float(input("Temperature at "+str(i)+":00 hours:"))
                    #This is basically the sames as in your code
    
                    if tem < -50 or tem > 150:
    
                        print ("Enter a temperature between -50 and 130")
                        #Same logic as in your code, prints an error message
                        #when the temperature is out of bounds
                    else:
                        #If the temperature is valid, break out of the while loop
    
                        break
                except ValueError:
                    print("Enter a number")
            myTemperatureList.append(tem)
    
    def get(myTemperatureList):
    对于范围(24)内的i:
    尽管如此:
    #这样,当打印错误消息时,
    #只要用户愿意,就会再次提示用户输入
    #他们提供了错误的输入