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.
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.
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$
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.