1

Input the math expression such as 3+2*{1+2*[-4/(8-6)+7]} make the python program calculate. The problem is, it gives no output. How to fix this problem.

while True:
 try:
 express = input()
 express.replace("[", "(")
 express.replace("{", "(")
 express.replace("]", ")")
 express.replace("}", ")")
 print(eval(express))
 except:
 break
asked Aug 8, 2021 at 4:43

1 Answer 1

1

Update: After each replacement, we must update the expression, otherwise the change will only reflect to the given expression & after each replace statement it will change and only modify the given expression

while True:
 try:
 express = input()
 x = express.replace("[", "(")
 y = x.replace("{", "(")
 z = y.replace("]", ")")
 w = z.replace("}", ")")
 print(eval(w))
 except:
 break
answered Aug 8, 2021 at 5:13
Sign up to request clarification or add additional context in comments.

3 Comments

The input with "[" "{" is the requirement of this question
This answer could use some explanatory text.
instead, you should redefine the express variable to make the program more efficient, instead of constantly creating new variables.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.