我只是被这段代码弄糊涂了:
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