0

What I want to do is to input a formula, like 1+1, and print: The Answer of question n is : (The answer of the formula). For 1+1, it should print: The Answer of Question 1 is: 2 But what I get is, when I input 1+1, it prints: The Answer of Question 1 is: '1+1'

def prefix():
    typein=int(input("How many questions do you want to ask:"))
    number=1
    while number<typein+1:
        a=input("Question %r you want to ask"%(number))
        print(a)
        print("The Answer of Question %r is: %r" %(number,a))
        number+=1
prefix()

When I add

print(a)

after

a=input("Question %r you want to ask"%(number))

It shows that a is 1+1, not 2. So how can I let a become the answer of the formula but not the formula itself?

--------------------------Edit-----------------------

It asks people to input an int ,and some formulas. There is no problem for us to input an int for the first part of the code. But for the second part of the code, which asks as to input the formula, it comes wrong.

Yiling Liu
  • 666
  • 1
  • 6
  • 21

1 Answers1

0

Thanks to the answers.I know what happened right now, just like the question: Evaluating a mathematical expression in a string

So I just have to add an eval()

def prefix():
typein=int(input("How many questions do you want to ask:"))
number=1
while number<typein+1:
    a=eval(input("Question %r you want to ask"%(number)))
    print("The Answer of Question %r is: %r" %(number,a))
    number+=1
prefix()

It works! enter image description here

Yiling Liu
  • 666
  • 1
  • 6
  • 21
  • The answers to the duplicate questions make it very clear: `eval` is evil. They recommend **not** to use it, and give many better, safer alternatives. – Thierry Lathuille Aug 01 '18 at 18:24