令人困惑的python - 无法将字符串转换为float

Tur*_*rbo -8 python error-handling

我有一个值错误,即使我尝试使用代码,它也不起作用!

我该怎么做对吗? - 我正在使用Python 3.3.2!

这是代码: 码

如您所见,该程序会询问您可以行走多少英里,并根据您输入的内容为您提供响应.

这是文本格式的代码:

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")
Run Code Online (Sandbox Code Playgroud)

Sim*_*ser 7

您需要考虑用户可能没有填写正确的值:

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

A try/except处理尝试将输入转换为float ValueError时可能发生的情况float.