[英]cannot convert string to float python value error
我正在介绍python类,所以我并不了解。 我正在处理食谱计算器,并且不断遇到错误,指出:追溯(最近一次通话过去):
File "/Users/Haley/Desktop/Python/assignment 2.py", line 6, in <module>
ing1amount = input(float("Please enter amount of ingredient 1"))
ValueError: could not convert string to float: 'Please enter amount of ingredient 1'
我不知道这意味着什么或如何真正解决它,所以任何帮助。 谢谢!
#Let the user input the name of the recipe
recipe = (input("Enter the name of your recipe: "))
#Let the user input ingredients and their amounts
ingredient1 = input("Please enter ingredient 1: ")
ing1amount = input(float("Please enter amount of ingredient 1"))
ingredient2 = input("Please enter ingredient 2: ")
ing2amount = input(float("Please enter amount of ingredient 2"))
...
您试图将"Please enter amount of ingredient 1"
为浮点数
ing1amount = float(input("Please enter amount of ingredient 1"))
#Let the user input ingredients and their amounts
ingredient1 = input("Please enter ingredient 1: ")
ing1amount = input(float("Please enter amount of ingredient 1"))
您的第一行将输入作为字符串。 第二行应将该字符串转换为浮点数。 但是,您没有选择使用第一行的结果,而是选择再次要求输入...但是您决定将提示字符串转换为浮点数,这将不起作用。 电脑必须解释
float(“请输入成分1的数量”)
才可以继续。 那句话不是合法的float
,所以程序大喊。 您需要使用第一行中的内容,例如:
ingredient1 = input("Please enter ingredient 1: ")
ing1amount = float(ingredient1)
我认为您只需要更改float和input的顺序即可。
ing1amount = float(input("Please enter amount of ingredient 1"))
函数input()将在命令提示符处提示用户,然后返回用户键入的内容,然后将其与float()进行包装会将结果强制转换为float,从而ing1amount将为浮点数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.