我无法计算我的答案,并且总是给出答案错误

时间:2019-03-05 12:49:29

标签: python python-3.x

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")

1 个答案:

答案 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..