私はこのコードに本当に混乱しました:
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, in if float(miles) <= 0: NameError: name 'miles' is not defined
私はPython 3.3.2を使用しています