November 17th, 2013, 05:54 PM
How To Convert String To Float?
I'm currently working on a program in Python and I need to figure out how to convert a string value to a float value.
The program will ask the user to enter a number, and uses a loop to continue asking for more numbers. The user must enter 0 to stop the loop (at which point, the program will give the user the average of all the numbers they entered).
What I want to do is allow the user to enter the word 'stop' instead of 0 to stop the loop. I've tried making a variable for stop = 0, but this causes the program to give me the following error message:
ValueError: could not convert string to float: 'stop'
So how do I make it so that 'stop' can be something the user can enter to stop the loop? Please let me know what I can do to convert the string to float. Thank you so much for your help!
November 17th, 2013, 05:59 PM
Function float() will do.
x = float(input("Enter a number: "))
Last edited by Dietrich; November 17th, 2013 at 06:04 PM.
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
November 17th, 2013, 06:04 PM
Can you please elaborate? Where exactly do I put this line of code? What do I do with it? What goes in the parentheses? Please help me! Thanks!
Originally Posted by Dietrich
November 17th, 2013, 08:35 PM
Put that line of code into a file.
[/code] are essential for python code and Makefiles!
November 17th, 2013, 08:40 PM
No worries, I figured it out. Thanks, though.
Originally Posted by b49P23TIvg