问题描述
我有一个值错误,即使我尝试玩代码,它不起作用。
我如何得到正确的? - 我使用的是Python 3.3.2!
这里是代码:
如您所见,程序会询问您可以走几英里,并根据您输入的内容给出回应。 p>
这是文本格式的代码:
print(欢迎来到
miles = input(你可以走几英里?)
如果float(英里)< = 0:
print(你以为你是谁?!!现在就走1000英里!)
elif float(miles)> = 10:
print(你很健康!保持起来!
elif float(miles)> 0和里程<
print(Good,Try doing 10 miles)
else:
print(请输入一个数字!)
miles = float(input你可以走几英里?))
如果里程< = 0:
print(你认为你是谁?现在去走1000英里!)
elif miles> = 10:
print(你很健康!保持起来!)
elif miles> 0和里程< 10:
打印(好,尝试做10英里)
这个问题正是Traceback日志所说的:无法将字符串转换为float
- 如果您只有一个字符串,只有数字,python足够聪明地做你想要的,并将字符串转换为一个浮动。
- 如果你有一个字符串使用非数字字符,转换将失败,并提供您所遇到的错误。
大多数人会接近的方式问题是使用 try / except
(请参阅),或使用 isdigit()
函数(请参阅)。
尝试/除外
try:
miles = float(input(你可以走几英里?))
ex cept:
print(请输入一个数字!)
Isdigit ()
miles = input(你可以走几英里?)
if不是miles.isdigit():
print(请输入一个数字!)
请注意,如果字符串中有小数点,则后者仍将返回false
编辑
好吧,我将无法再回到你一段时间,所以我会发布答案,以防万一。
while真的:
try:
miles = float(input(你可以走几英里?))
break
除了:
print(请输入一个数字!)
#所有的ifs和东西
代码很简单:
- 它将继续尝试将输入转换为浮点数,如果失败,循环回到开始。 li>
- 最终成功后,会破坏f循环并转到你放下的代码。
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:
As you can see, the program asks for how many miles you can walk and gives you a response depending on what you type in.
This is the code in text format:
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")
The problem is exactly what the Traceback log says: Could not convert string to float
- If you have a string with only numbers, python's smart enough to do what you're trying and converts the string to a float.
- If you have a string with non-numerical characters, the conversion will fail and give you the error that you were having.
The way most people would approach this problem is with a try/except
(see here), or using the isdigit()
function (see here).
Try/Except
try:
miles = float(input("How many miles can you walk?: "))
except:
print("Please type in a number!")
Isdigit()
miles = input("How many miles can you walk?: ")
if not miles.isdigit():
print("Please type a number!")
Note that the latter will still return false if there are decimal points in the string
EDIT
Okay, I won't be able to get back to you for a while, so I'll post the answer just in case.
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
The code's really simple:
- It will keep trying to convert the input to a float, looping back to the beginning if it fails.
- When eventually it succeeds, it'll break from the loop and go to the code you put lower down.
这篇关于混淆python - 无法将字符串转换为float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!