我正在介绍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"))
...
答案 0 :(得分:1)
您尝试将"Please enter amount of ingredient 1"
转换为浮点数
ing1amount = float(input("Please enter amount of ingredient 1"))
答案 1 :(得分:1)
#Let the user input ingredients and their amounts
ingredient1 = input("Please enter ingredient 1: ")
ing1amount = input(float("Please enter amount of ingredient 1"))
您的第一行将输入作为字符串。第二行应该将该字符串转换为浮点数。但是,您没有选择使用第一行的结果,而是选择再次要求输入...但是您决定将 prompt 字符串转换为浮点数,这将不起作用。电脑必须翻译
float(“请输入成分1的数量”)
在它可以继续之前。该句子不是合法的float
,因此该程序大喊大叫。您需要使用第一行中的内容,例如:
ingredient1 = input("Please enter ingredient 1: ")
ing1amount = float(ingredient1)
答案 2 :(得分:0)
我认为您只需要更改float和input的顺序即可。
ing1amount = float(input("Please enter amount of ingredient 1"))
input()函数将在命令提示符下提示用户,然后返回用户键入的内容,然后将其与float()包装在一起,将结果转换为float,从而ing1amount将为浮点数。