Confusing python - Cannot convert string to float

后端 未结 4 2205
小蘑菇
小蘑菇 2020-12-11 14:11

I got a value error and even if I try playing around with the code, it doesn\'t work!

How can I get it right? - I am using Python 3.3.2!

Here is the code:

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-11 14:53

    You need to take into account that the user might not fill in a proper value:

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

    A try/except handles the ValueError that might occur when float tries to convert the input to a float.

提交回复
热议问题