Home > database >  Python- How to convert a string to float ( i know what float() function is my question is different)
Python- How to convert a string to float ( i know what float() function is my question is different)

Time:11-14

I have a problem converting string to float. For example, I want to get an input like 10**2 and I want the program to save the result of 10**2 in the variable.

like this :

Number = float(input("Enter the number : "))
print(number * 2)

something like this and when i run and it says :

Enter the number :

and I give it 10**2 I want it to return 200

like this :

Enter the number : 10**2
200

How can I do it? I tried different ways non worked.

CodePudding user response:

You can use eval to parse general arithmetic expressions:

Number = eval(input("Enter the number : "))
print(Number * 2)

You can even provide formulas, such as 10**2 5, etc.

CodePudding user response:

Change "number" to Number", and format the print as follows:

Mac_3.2.57$cat readIn.py
Number = float(input("Enter the number : "))
print("%.0f\n" % (Number * 2))
Mac_3.2.57$python readIn.py
Enter the number : 10**2
200

Mac_3.2.57$
  • Related