1

My program prompts the user to input the volume of a sphere. This is one of the example input I have to try:

  • 1.4*1.0e6

This is what I have as the prompt right now: volume = float(input("Enter volume of sphere in mm^3: "))

And I got this error: ValueError: could not convert string to float: '1.4*1.0e6'

Why is it that when I directly assign the value of a variable like a = 1.4*1.0e6, it's a float but when the user inputs, it's a string? How do I convert it to float? Is there any built-in functions to do that?

Sorry for my bad English and thank you.

hoshimani
  • 13
  • 3
  • Please check these answers to ["How do I parse a string to a float or int?"](https://stackoverflow.com/q/379906/2314737): https://stackoverflow.com/a/20929983/2314737 and https://stackoverflow.com/a/17815252/2314737 – user2314737 Sep 09 '21 at 07:04
  • `1.4 * 1.0e6` is not directly a float: it's a multiplication of two floats. When you write it in code, the multiplication is evaluated, and the result is a float. But if you call `float('1.4 * 1.0e6')`, you will get an error, because `float( )` can converts strings into floats, not convert strings into multiplications then perform the multiplication. Note that `1.4 * 1.0e6` will evaluate to the same number as `1.4e6`; and `float('1.4e6')` will work as you want it to. So, just tell your user to enter the expression of a float directly, rather than a multiplication of two floats. – Stef Sep 09 '21 at 09:44

4 Answers4

0

ValueError: could not convert string to float: '1.4*1.0e6' you try to convert formula to float. the most easy way is convert string to formula using eval

volume = eval(input("Enter volume of sphere in mm^3: "))

galaxyan
  • 5,527
  • 2
  • 18
  • 41
0
s = '10.5674'
f = float(s)
print(type(f))
print('Float Value =', f)

Output:

<class 'float'>
Float Value = 10.5674.

This way you can convert string to float. Also, in your case you write * between numbers so it can't convert in proper way. You have to assign float number in different variables then perform an operation.

ZygD
  • 10,844
  • 36
  • 65
  • 84
0

For converting a string to float you can use float(). Eg.float('1.2123') You can not convert '*' into float because its an operator that's why error is coming.

#observe the code
volume = float(input("Enter volume of sphere in mm^3: "))

input() = Takes input from user and stores it in string

float() = It is used to convert strings to float

Your code is volume = float(input("Enter volume of sphere in mm^3: "))

1st input() takes input from user in string then float() converts it into float.

But a = 1.4*1.0e6 there is no input() function involved thats why it's a float.

You can simply achieve your work by

volume = eval(input("Enter volume of sphere in mm^3: "))

OR

volume = input("Enter volume of sphere in mm^3: ").split("*")
volume = float(volume[0])*float(volume[1])

split(separator)- It splits a string into an array of sub-strings, by a separator string provided by user

You can read about eval from here:- Read abut eval() from here

Abhay Kumar
  • 91
  • 1
  • 12
-1

to convert a string to a float, the string should consist of a float number.

In your case it is a multiplication of 2 floats.

Try:

eval('1.4*1.0e6')

This will give you a floating point number, as it evaluates the string and does the multiplication.

Had the user input been '1.4e6', then the float would have worked:

try:

float('1.4e6')
SharonShaji
  • 117
  • 6
  • 1
    I wouldn't recommend using `eval` on user input. The user could easily damage your system. – Stef Sep 09 '21 at 09:46