def add(x, y):
return x + y
def sub(x, y):
return x - y
def multiply(x, y ):
return x * y
def div(x, y):
return x / y
print("Select Operation:")
print("1.add")
print("2.sub")
print("3.multiply")
print("4.div")
choice = input("enter your operation number: ")
a = int(input("enter the first number:"))
b = int(input("enter the second number: "))
if choice == 1:
print(a,"+",b,"=", add(a,b))
elif choice == 2:
print(a,"-",b,"=", sub(a,b))
elif choice == 3:
print(a,"*",b,"=", multiply(a,b))
elif choice == 4:
print(a,"/",b,"=", div(a,b))
else:
print("err")
答案 0 :(得分:1)
注意,输入内容时,它是字符串。
因此,当执行以下操作时:choice == 1
,它会尝试执行:'1' == 1
,这是错误的
要做:
if choice == '1':
print(a,"+",b,"=", add(a,b))
elif choice == '2':
print(a,"-",b,"=", sub(a,b))
elif choice == '3':
print(a,"*",b,"=", multiply(a,b))
elif choice == '4':
# complete..