0

I trying to write code to show how high a ball would go up from the calculation however no matter how I type it, it keeps saying that it "cannot convert string to float" on the height varible

time = input("How long did the ball go up? in seconds\n")
time = float(time)

velocity = input("What was the initial velocity?\n")
velocity = float(velocity)

height = ("What was the initial height?\n")
height = float(height)

answer = (time ** 2 * -16) + (velocity * time) + height

print(answer)

ValueError: could not convert string to float: 'What was the initial height?\n'

why does the code not want to convert?

3
  • 1
    You missed input to read height. Commented Sep 7, 2018 at 17:57
  • typo: you want height = input("What was the initial height?\n") Commented Sep 7, 2018 at 18:09
  • 1
    damn didn't see that, thank you for helping me the code works now Commented Sep 7, 2018 at 18:41

1 Answer 1

0

This is happening because time is a string. You can't change the datatype, but you can cast it. What you should do instead is:

time = input("How long did the ball go up? in seconds\n")

velocity = input("What was the initial velocity?\n")

height = input("What was the initial height?\n")

answer = (float(time) ** 2 * -16) + (float(velocity) * float(time)) + float(height)

print(answer)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.