在 Try Except 上使用 While 循环
Using a While Loop on a Try Except
我正在使用 Python 3.4.3。我已经成功地编写了在 for 循环中使用 try except 的代码,以确保我的列表中填充了我正在寻找的数据。我遇到的问题是在发生异常后,当输入导致另一个异常错误时,它不会重复代码以重新提示用户输入。
def getTemperatures():
times=["00:00","01:00","02:00","03:00","04:00","05:00","06:00","07:00",
"08:00","09:00","10:00","11:00","12:00","13:00","14:00","15:00",
"16:00","17:00","18:00","19:00","20:00","21:00","22:00","23:00"]
hourlyTemperatures=[]*len(times)
for x in times:
print(x,"Hours")
while True:
try:
hourlyTemps=eval(input("""Please input your temperature in fahrenheit
(round to the nearest degree) for this hour:\t"""))
hourlyTemperatures.append(hourlyTemps)
print("\n")
except (NameError,SyntaxError,TypeError):
print("\n")
print("You did not input a number.")
hourlyTemps=eval(input("""Please input your temperature in fahrenheit
(round to the nearest degree) for this hour:\t"""))
hourlyTemperatures.append(hourlyTemps)
print("\n")
while hourlyTemps<-50 or hourlyTemps>130:
hourlyTemperatures.remove(hourlyTemps)
print("That is a temperature outside human habitable environments.")
hourlyTemps=eval(input("""Please input your temperature in fahrenheit
(round to the nearest degree) for this hour:\t"""))
hourlyTemperatures.append(hourlyTemps)
print("\n")
print(hourlyTemperatures)
getTemperatures()
我试过将 while 循环放在 for 循环之外,以及在 try 之后但在 except 之前,但我无法让它工作。
这是我测试代码时得到的输出示例:
00:00 Hours
Please input your temperature in fahrenheit
(round to the nearest degree) for this hour:
You did not input a number.
Please input your temperature in fahrenheit
(round to the nearest degree) for this hour:
Traceback (most recent call last):
File "F:.4.3 Python Programming\CS117\Labs\Lab03\GetTemperatures.py", line 11, in getTemperatures
(round to the nearest degree) for this hour:\t"""))
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "F:.4.3 Python Programming\CS117\Labs\Lab03\GetTemperatures.py", line 34, in <module>
getTemperatures()
File "F:.4.3 Python Programming\CS117\Labs\Lab03\GetTemperatures.py", line 19, in getTemperatures
(round to the nearest degree) for this hour:\t"""))
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
我很感激任何人可能必须提供的关于如何解决这个问题的任何建议。
将else
与try
语句一起使用,确保只有在没有异常发生时才会执行以下代码。
此外,您的代码无法退出 while
循环,因此我建议在您需要退出 while
循环并继续 for
的地方添加 break
。
此外,检查温度是否在边界内的 while
按照您的说法几乎没有用。在您的代码中,临时文件被附加到列表中,然后运行检查。所以改为这样做:
while True:
try:
hourlyTemps = float(input("""Please input your temperature in fahrenheit
(round to the nearest degree) for this hour:\t""")) # the eval is unneccesary, the number check is simpler
if -50 < hourlyTemps < 130:
hourlyTemperatures.append(hourlyTemps)
break
except (TypeError, ValueError):
print() # putting the \n char is also unneccesary
print("You did not input a number.")
print()
就这么简单。如果不清楚,请提出问题。祝你好运!
我正在使用 Python 3.4.3。我已经成功地编写了在 for 循环中使用 try except 的代码,以确保我的列表中填充了我正在寻找的数据。我遇到的问题是在发生异常后,当输入导致另一个异常错误时,它不会重复代码以重新提示用户输入。
def getTemperatures():
times=["00:00","01:00","02:00","03:00","04:00","05:00","06:00","07:00",
"08:00","09:00","10:00","11:00","12:00","13:00","14:00","15:00",
"16:00","17:00","18:00","19:00","20:00","21:00","22:00","23:00"]
hourlyTemperatures=[]*len(times)
for x in times:
print(x,"Hours")
while True:
try:
hourlyTemps=eval(input("""Please input your temperature in fahrenheit
(round to the nearest degree) for this hour:\t"""))
hourlyTemperatures.append(hourlyTemps)
print("\n")
except (NameError,SyntaxError,TypeError):
print("\n")
print("You did not input a number.")
hourlyTemps=eval(input("""Please input your temperature in fahrenheit
(round to the nearest degree) for this hour:\t"""))
hourlyTemperatures.append(hourlyTemps)
print("\n")
while hourlyTemps<-50 or hourlyTemps>130:
hourlyTemperatures.remove(hourlyTemps)
print("That is a temperature outside human habitable environments.")
hourlyTemps=eval(input("""Please input your temperature in fahrenheit
(round to the nearest degree) for this hour:\t"""))
hourlyTemperatures.append(hourlyTemps)
print("\n")
print(hourlyTemperatures)
getTemperatures()
我试过将 while 循环放在 for 循环之外,以及在 try 之后但在 except 之前,但我无法让它工作。
这是我测试代码时得到的输出示例:
00:00 Hours
Please input your temperature in fahrenheit
(round to the nearest degree) for this hour:
You did not input a number.
Please input your temperature in fahrenheit
(round to the nearest degree) for this hour:
Traceback (most recent call last):
File "F:.4.3 Python Programming\CS117\Labs\Lab03\GetTemperatures.py", line 11, in getTemperatures
(round to the nearest degree) for this hour:\t"""))
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "F:.4.3 Python Programming\CS117\Labs\Lab03\GetTemperatures.py", line 34, in <module>
getTemperatures()
File "F:.4.3 Python Programming\CS117\Labs\Lab03\GetTemperatures.py", line 19, in getTemperatures
(round to the nearest degree) for this hour:\t"""))
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
我很感激任何人可能必须提供的关于如何解决这个问题的任何建议。
将else
与try
语句一起使用,确保只有在没有异常发生时才会执行以下代码。
此外,您的代码无法退出 while
循环,因此我建议在您需要退出 while
循环并继续 for
的地方添加 break
。
此外,检查温度是否在边界内的 while
按照您的说法几乎没有用。在您的代码中,临时文件被附加到列表中,然后运行检查。所以改为这样做:
while True:
try:
hourlyTemps = float(input("""Please input your temperature in fahrenheit
(round to the nearest degree) for this hour:\t""")) # the eval is unneccesary, the number check is simpler
if -50 < hourlyTemps < 130:
hourlyTemperatures.append(hourlyTemps)
break
except (TypeError, ValueError):
print() # putting the \n char is also unneccesary
print("You did not input a number.")
print()
就这么简单。如果不清楚,请提出问题。祝你好运!