Python 3.x 在Python中实现try/except?

Python 3.x 在Python中实现try/except?,python-3.x,Python 3.x,我被这段代码弄糊涂了: print("Welcome to Healthometer, powered by Python...") miles = input("How many miles can you walk?: ") if float(miles) <= 0: print("Who do you think you are?!! Go and walk 1000 miles now!") elif float(miles) >= 10: print("Yo

我被这段代码弄糊涂了:

print("Welcome to Healthometer, powered by Python...")
miles = input("How many miles can you walk?: ")
if float(miles) <= 0:
    print("Who do you think you are?!! Go and walk 1000 miles now!")
elif float(miles) >= 10:
    print("You are very healthy! Keep it up!")
elif float(miles) > 0 and miles < 10:
    print("Good. Try doing 10 miles")
else:
    print("Please type in a number!")
    miles = float(input("How many miles can you walk?: "))
    if miles <= 0:
        print("Who do you think you are?!! Go and walk 1000 miles now!")
    elif miles >= 10:
        print("You are very healthy! Keep it up!")
    elif miles > 0 and miles < 10:
        print("Good. Try doing 10 miles")
print(“欢迎使用HealthMeter,由Python提供支持…”)
英里=输入(“你能走多少英里?:”)
如果浮动(英里)=10:
打印(“你很健康!坚持下去!”)
elif浮动(英里数)>0且英里数<10:
打印(“很好,试着跑10英里”)
其他:
打印(“请键入一个数字!”)
英里=浮动(输入(“你能走多少英里?”:”)
如果英里数=10:
打印(“你很健康!坚持下去!”)
如果英里数大于0且英里数小于10:
打印(“很好,试着跑10英里”)
但是,请查看错误:

有人告诉我用Python试试try/except,但是在看了文档之后,我不知道该做什么,以及如何用代码中的所有其他ifs和elif实现它。 我试着这样做:

print("Welcome to Healthometer, powered by Python...")
try:
    miles = float(input("How many miles can you walk? "))
except ValueError:
    print("That is not a valid number of miles")

if float(miles) <= 0:
    print("Who do you think you are?!! Go and walk 1000 miles now!")
elif float(miles) >= 10:
    print("You are very healthy! Keep it up!")
elif float(miles) > 0 and miles < 10:
    print("Good. Try doing 10 miles")
else:
    print("Please type in a number!")
    miles = float(input("How many miles can you walk?: "))
    if miles <= 0:
        print("Who do you think you are?!! Go and walk 1000 miles now!")
    elif miles >= 10:
        print("You are very healthy! Keep it up!")
    elif miles > 0 and miles < 10:
        print("Good. Try doing 10 miles")
print(“欢迎使用HealthMeter,由Python提供支持…”)
尝试:
英里=浮动(输入(“你能走多少英里?”)
除值错误外:
打印(“这不是有效的英里数”)
如果浮动(英里)=10:
打印(“你很健康!坚持下去!”)
elif浮动(英里数)>0且英里数<10:
打印(“很好,试着跑10英里”)
其他:
打印(“请键入一个数字!”)
英里=浮动(输入(“你能走多少英里?”:”)
如果英里数=10:
打印(“你很健康!坚持下去!”)
如果英里数大于0且英里数小于10:
打印(“很好,试着跑10英里”)
但它给出了:第7行,在
如果float(miles)发生的情况可能是您陷入了except块,因此
miles
变量永远不会被声明,代码的其余部分需要该变量。通过添加
raise
,它将强制您的代码退出,以便在输入出现问题时,其余代码永远不会运行

try:
    miles = float(input("How many miles can you walk? "))
except Exception, e:
    print("That is not a valid number of miles")
    raise e
编辑:

我知道你现在要做什么了。这将使您的代码按预期工作。顺便说一句,你处理这个问题的方式不是很好。你需要阅读更多关于如何处理用户输入的内容

miles = None
try:
    miles = float(input("How many miles can you walk? "))
except ValueError:
    print("That is not a valid number of miles")

可能发生的情况是,您陷入了except块,因此,
miles
变量永远不会被声明,代码的其余部分需要该变量。通过添加
raise
,它将强制您的代码退出,以便在输入出现问题时,其余代码永远不会运行

try:
    miles = float(input("How many miles can you walk? "))
except Exception, e:
    print("That is not a valid number of miles")
    raise e
编辑:

我知道你现在要做什么了。这将使您的代码按预期工作。顺便说一句,你处理这个问题的方式不是很好。你需要阅读更多关于如何处理用户输入的内容

miles = None
try:
    miles = float(input("How many miles can you walk? "))
except ValueError:
    print("That is not a valid number of miles")

可能发生的情况是,您陷入了except块,因此,
miles
变量永远不会被声明,代码的其余部分需要该变量。通过添加
raise
,它将强制您的代码退出,以便在输入出现问题时,其余代码永远不会运行

try:
    miles = float(input("How many miles can you walk? "))
except Exception, e:
    print("That is not a valid number of miles")
    raise e
编辑:

我知道你现在要做什么了。这将使您的代码按预期工作。顺便说一句,你处理这个问题的方式不是很好。你需要阅读更多关于如何处理用户输入的内容

miles = None
try:
    miles = float(input("How many miles can you walk? "))
except ValueError:
    print("That is not a valid number of miles")

可能发生的情况是,您陷入了except块,因此,
miles
变量永远不会被声明,代码的其余部分需要该变量。通过添加
raise
,它将强制您的代码退出,以便在输入出现问题时,其余代码永远不会运行

try:
    miles = float(input("How many miles can you walk? "))
except Exception, e:
    print("That is not a valid number of miles")
    raise e
编辑:

我知道你现在要做什么了。这将使您的代码按预期工作。顺便说一句,你处理这个问题的方式不是很好。你需要阅读更多关于如何处理用户输入的内容

miles = None
try:
    miles = float(input("How many miles can you walk? "))
except ValueError:
    print("That is not a valid number of miles")

如果输入无效,将执行except语句,但脚本将继续执行该语句。你有两个选择。您可以告诉python使用sys.exit()退出应用程序,也可以实现一些其他行为,例如告诉用户重试或默认为某个硬编码值

像这样使用exit:

import sys

try:
    miles = float(input("How many miles can you walk? "))
except Exception, e:
    print("That is not a valid number of miles")
    sys.exit()
或者,您可以要求用户重试:

miles = None
def ask():
    global miles
    try:
        miles = float(input("How many miles can you walk? "))
    except: 
        print("Invalid input, try again")
        ask()

ask()
# remaining code here

如果输入无效,将执行except语句,但脚本将继续执行该语句。你有两个选择。您可以告诉python使用sys.exit()退出应用程序,也可以实现一些其他行为,例如告诉用户重试或默认为某个硬编码值

像这样使用exit:

import sys

try:
    miles = float(input("How many miles can you walk? "))
except Exception, e:
    print("That is not a valid number of miles")
    sys.exit()
或者,您可以要求用户重试:

miles = None
def ask():
    global miles
    try:
        miles = float(input("How many miles can you walk? "))
    except: 
        print("Invalid input, try again")
        ask()

ask()
# remaining code here

如果输入无效,将执行except语句,但脚本将继续执行该语句。你有两个选择。您可以告诉python使用sys.exit()退出应用程序,也可以实现一些其他行为,例如告诉用户重试或默认为某个硬编码值

像这样使用exit:

import sys

try:
    miles = float(input("How many miles can you walk? "))
except Exception, e:
    print("That is not a valid number of miles")
    sys.exit()
或者,您可以要求用户重试:

miles = None
def ask():
    global miles
    try:
        miles = float(input("How many miles can you walk? "))
    except: 
        print("Invalid input, try again")
        ask()

ask()
# remaining code here

如果输入无效,将执行except语句,但脚本将继续执行该语句。你有两个选择。您可以告诉python使用sys.exit()退出应用程序,也可以实现一些其他行为,例如告诉用户重试或默认为某个硬编码值

像这样使用exit:

import sys

try:
    miles = float(input("How many miles can you walk? "))
except Exception, e:
    print("That is not a valid number of miles")
    sys.exit()
或者,您可以要求用户重试:

miles = None
def ask():
    global miles
    try:
        miles = float(input("How many miles can you walk? "))
    except: 
        print("Invalid input, try again")
        ask()

ask()
# remaining code here

我回答了你的另一个问题,但我想我也可以在这里回答

while True:
    try:
        miles = float(input("How many miles can you walk?: "))
        break
    except:
        print("Please type in a number!")

#All of the ifs and stuff
#Make sure not to put these in the loop, they go AFTER!!
代码非常简单:

  • 它将继续尝试将输入转换为浮点,如果失败,将循环回到开始
  • 当它最终成功时,它将从循环中中断并转到您放在下面的代码
编辑: 为了进一步解释,您得到错误
NameError:name“miles”未定义的原因是
Try/Except
将失败,并且不会将
miles
定义为输入。相反,它会打印“请键入…”,然后继续执行
if
s

这就是为什么你需要这个循环。它使您的代码保持t