0

我只是被这段代码弄糊涂了:

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

但是,看看错误:http: //i.stack.imgur.com/PYT80.png

有人告诉我在 Python 中尝试 try/except,尽管在查看文档之后,我不知道该做什么以及如何使用代码中的所有其他 if 和 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")

但它给出了:第 7 行,在 if float(miles) <= 0: NameError: name 'miles' is not defined

我正在使用 Python 3.3.2

4

3 回答 3

2

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

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' is not defined是因为Try/Except会失败,并且不会定义miles为输入。相反,它会打印“请输入...”,然后继续执行ifs 。

这就是为什么你需要循环。它使您的代码不断尝试定义miles,直到成功,然后才从循环中中断并进入逻辑。

于 2013-11-01T23:29:34.513 回答
1

发生的事情可能是您陷入了 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")
于 2013-11-01T23:21:08.437 回答
0

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

像这样使用退出:

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
于 2013-11-01T23:21:28.547 回答