0

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
  • 5
  • 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

3 Answers3

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
  • 116
  • 1
  • 12
-1

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,944
  • 2
  • 19
  • 43
-1
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
  • 22,092
  • 39
  • 79
  • 102