我被困在如何在变量中进行计算。目前我的代码将打印(例如):( 1,'+',2)。但我需要答案的实际总和..我的代码:
def calc(num1,op,num2):
sum = num1,op,num2
print(sum)
num1 = int(input("First number:"))
op = input("Operator:")
num2 = int(input("Second number:"))
calc(num1,op,num2)
答案 0 :(得分:0)
这可以帮到你......
def calc(x=0, y=0, z=0):
expression = raw_input('Enter an expression: ')
return eval(expression, None, locals())
示例:
>>> calc()
Enter an expression: 8 + 5 - 7
6