Python 如何输入公式并得到结果?

Python 如何输入公式并得到结果?,python,Python,我想做的是输入一个公式,比如1+1,然后打印:问题n的答案是:(公式的答案)。对于1+1,它应该打印:问题1的答案是:2 但我得到的是,当我输入1+1时,它会打印:问题1的答案是:“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

我想做的是输入一个公式,比如1+1,然后打印:问题n的答案是:(公式的答案)。对于1+1,它应该打印:问题1的答案是:2 但我得到的是,当我输入1+1时,它会打印:问题1的答案是:“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()
之后

a=input("Question %r you want to ask"%(number))
它表明a是1+1,而不是2。 那么我怎么能让a成为公式的答案而不是公式本身呢

--------------------------编辑-----------------------


它要求人们输入一个整数和一些公式。我们可以为代码的第一部分输入int。但是对于代码的第二部分,它要求输入公式,它是错误的

多亏了这些答案。我知道现在发生了什么,就像问题一样:

所以我只需要添加一个eval()

def prefix():
typein=int(输入(“您想问多少问题:”)
数字=1

你想考虑哪些操作符?你在这些行上输入了什么?提示:EVE(‘1+1’)返回2。这应该适用于最基本的表达式,如:
。如果我运行您的代码并将
键入为
1
,我会得到以下输出
问题1的答案是:'1'
。重复问题的答案非常清楚:
eval
是邪恶的。他们建议不要使用它,并提供了许多更好、更安全的替代方案。
a=input("Question %r you want to ask"%(number))
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()