在Try除外使用While循环

时间:2016-02-04 07:03:04

标签: python-3.x while-loop syntax-error try-except

我正在使用Python 3.4.3。我已经成功编写了一个使用try的代码,除了在for循环中,以确保我的列表正在填充我正在寻找的数据。我遇到的麻烦是在发生异常后,当输入导致另一个异常错误时,它不会重复代码来重新提示用户输入。

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:\3.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:\3.4.3 Python Programming\CS117\Labs\Lab03\GetTemperatures.py", line 34, in <module>
    getTemperatures()
  File "F:\3.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

我感谢任何人可能提供的有关如何解决此问题的建议。

1 个答案:

答案 0 :(得分:0)

elsetry语句一起使用,以确保只有在没有异常发生时才会执行以下代码。 此外,您的代码无法退出while循环,因此我建议您在需要退出break循环的地方添加while并继续for。 此外,检查温度是否在边界内的while与您放置它的方式相比毫无用处。在您的代码中,temp会附加到列表中,然后运行检查。所以反而像这样做:

    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()

就这么简单。如果问题不明确,请提问。祝你好运!