Haley
Haley

Reputation: 39

cannot convert string to float python value error

I'm in an intro to python class so I don't know much. I'm working on a recipe calculator assignment and I keep on running into an error that states: Traceback (most recent call last):

  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'

I don't know what this means or how to really fix it, so anything helps. Thanks!

#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"))
...

Upvotes: 1

Views: 314

Answers (3)

Prune
Prune

Reputation: 77910

#Let the user input ingredients and their amounts
ingredient1 = input("Please enter ingredient 1: ")
ing1amount = input(float("Please enter amount of ingredient 1"))

Your first line gets the input as a string. The second line should convert that string to a float. however, instead of using your result from that first line, you chose to ask for the input again ... but the you decided to convert the prompt string to a float, which isn't going to work. The computer has to interpret

float("Please enter amount of ingredient 1")

Before it can continue. That sentence is not a legal float, so the program yells. What you need is to use what you got on the first line, like so:

ingredient1 = input("Please enter ingredient 1: ")
ing1amount = float(ingredient1)

Upvotes: 1

user10356004
user10356004

Reputation:

You tried to convert "Please enter amount of ingredient 1" to float

ing1amount = float(input("Please enter amount of ingredient 1"))

Upvotes: 1

Michael
Michael

Reputation: 566

I think you just need to change the order of float and input.

ing1amount = float(input("Please enter amount of ingredient 1"))

The function input() will prompt the user at the command prompt and then return what the user types and then wrapping that with float() will cast the result to a float so that ing1amount will be floating point.

Upvotes: 0

Related Questions