Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在Try-Except上使用While循环_Python 3.x_While Loop_Syntax Error_Try Except - Fatal编程技术网

Python 3.x 在Try-Except上使用While循环

Python 3.x 在Try-Except上使用While循环,python-3.x,while-loop,syntax-error,try-except,Python 3.x,While Loop,Syntax Error,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:0

我正在使用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()
def getTemperatures():
时间=[“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"]
小时温度=[]*len(次)
对于x的时间:
打印(x,“小时”)
尽管如此:
尝试:
hourlyTemps=eval(输入(““”请输入您的华氏温度
(四舍五入到最接近的度数)本小时:\t“”)
hourlyTemperatures.append(hourlyTemps)
打印(“\n”)
除了(名称错误、语法错误、类型错误):
打印(“\n”)
打印(“您没有输入数字。”)
hourlyTemps=eval(输入(““”请输入您的华氏温度
(四舍五入到最接近的度数)本小时:\t“”)
hourlyTemperatures.append(hourlyTemps)
打印(“\n”)
而hourlyTemps130:
小时温度。移除(小时温度)
打印(“这是人类居住环境以外的温度。”)
hourlyTemps=eval(输入(““”请输入您的华氏温度
(四舍五入到最接近的度数)本小时:\t“”)
hourlyTemperatures.append(hourlyTemps)
打印(“\n”)
打印(小时温度)
获取温度()
我已经尝试将while循环放置在for循环之外,以及尝试之后但在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
00:00小时
请输入您的华氏温度
(四舍五入到最接近的度数)本小时:
您没有输入数字。
请输入您的华氏温度
(四舍五入到最接近的度数)本小时:
回溯(最近一次呼叫最后一次):
文件“F:\3.4.3 Python编程\CS117\Labs\Lab03\GetTemperatures.py”,第11行,在GetTemperatures中
(四舍五入到最接近的度数)本小时:\t“”)
文件“”,第0行
^
SyntaxError:分析时出现意外的EOF
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“F:\3.4.3 Python编程\CS117\Labs\Lab03\GetTemperatures.py”,第34行,在
获取温度()
文件“F:\3.4.3 Python编程\CS117\Labs\Lab03\GetTemperatures.py”,第19行,在GetTemperatures中
(四舍五入到最接近的度数)本小时:\t“”)
文件“”,第0行
^
SyntaxError:分析时出现意外的EOF

我感谢任何人就如何解决此问题提供的任何建议。

使用
else
try
语句,以确保只有在没有异常发生时才会执行以下代码。 此外,您的代码无法退出
while
循环,因此我建议在需要退出
while
循环的位置添加
break
,然后继续执行
for
。 此外,检查温度是否在边界内的
while
按您的说法几乎是无用的。在您的代码中,temp被附加到列表中,然后检查运行。因此,smth应该这样做:

    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()
为True时:
尝试:
hourlyTemps=float(输入(““”请输入您的华氏温度
(四舍五入到最接近的度数)本小时:\t“”)#评估不必要,数字检查更简单
如果-50

就这么简单。如果有什么不清楚,请提问。祝你好运

谢谢你的意见。事实证明,我不需要if语句,并将其保留为while循环,但在第一个while循环之外也可以工作。我的其余代码现在与您的建议类似,因此感谢您的帮助!!!