Python 使用范围函数时的输入验证
我正在创建一个函数,将输入附加到列表中。一天中需要24个项目,我正在使用range函数来实现这一点。我需要验证这个输入。然而,我似乎不能让它每次都正确地验证,或者我已经得到了正确的验证,但是代码提示超过24个输入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
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小时:”)< /代码>
列表
作为变量。尝试使用描述其用途的变量,如myTemperatureList
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小时:”)< /代码> 列表
作为变量。尝试使用描述其用途的变量,如myTemperatureList
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:
尽管如此:
#这样,当打印错误消息时,
#只要用户愿意,就会再次提示用户输入
#他们提供了错误的输入