1

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.

2
  • 2
    10**2 is 100, not 200. Commented Nov 13, 2022 at 16:16
  • Ok, I can see your code and what you expect, but what does your code do and why is it not right? Commented Nov 13, 2022 at 16:20

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

might be worth a warning! a silly/naughty user could enter __import__('os').system('rm /') which might not be bad, but adding "-rf" in there probably would be
I guess that is concern if this is plugged into a website, but the use case of the OP seemed to be an everyday application. In that case, eval is the same as the user opening a terminal and typing commands.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.