变量中的计算总和
Sum of calculation in a variable
我对如何在变量中进行计算感到困惑。目前我的代码将打印(例如):(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)
这可以帮助你.....
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
我对如何在变量中进行计算感到困惑。目前我的代码将打印(例如):(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)
这可以帮助你.....
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