我的计算器代码未打印答案

时间:2019-05-11 15:16:27

标签: python

答案不会打印!

自从卡住以来,我没有尝试过任何东西

该程序创建一个简单的计算器

此功能将两个数字相加

def add_numbers(x,y):
    return x+y

此功能将一个数字与另一个数字相除

def subtract_numbers(x,y):
    return x-y

此功能将两个数字相乘

def multiply_numbers(x,y):
    return x*y

此功能将一个数字除以另一个

def divide_numbers(x,y):
    return x/y

接受用户输入

choice = int(input(" Enter 1 to add, 2 to subtract, 3 to multiply, 4 to divide. "))
num1 = int(input("What is your first number? "))
num2 = int(input("What is your second number? "))

if choice == '1':
    print ("yah")
    print ((num1) + "+" + (num2) + "=" + (add_numbers(num1,num2)))

elif choice == "2":
    print ((num1) + "-" + (num2) + "=" + (subtract_numbers(num1,num2)))

elif choice == "3":
   print ((num1) + "x" + (num2) + "=" + (multiply_numbers(num1,num2)))

elif choice == "4":
   print ((num1) + "/" + (num2) + "=" + (divide_numbers(num1,num2)))

我要打印答案,但不会打印

1 个答案:

答案 0 :(得分:1)

您的代码中几乎没有错误

  • 您将输入作为整数choice = int(input(...)),但与choice == '1'中的字符串进行比较,选择一个,我建议将整数与整数进行比较
    例如choice == 1

  • 当您尝试打印例如((num1) + "/" + (num2)不起作用,我建议使用字符串格式来打印输出,str.format 例如print('{}+{}={}'.format(num1, num2, add_numbers(num1, num2)))

因此,您的代码示例修复程序看起来像

#Taking input as int
choice = int(input(" Enter 1 to add, 2 to subtract, 3 to multiply, 4 to divide. "))
num1 = int(input("What is your first number? "))
num2 = int(input("What is your second number? "))

def add_numbers(x,y):
    return x+y

def subtract_numbers(x,y):
    return x-y

def multiply_numbers(x,y):
    return x*y

def divide_numbers(x,y):
    return x/y

#Comparing int to int
if choice == 1:
    print("yah")
    #Using string formatting to print the results
    print('{}+{}={}'.format(num1, num2, add_numbers(num1, num2)))

elif choice == 2:
    print('{}-{}={}'.format(num1, num2, subtract_numbers(num1, num2)))

elif choice == 3:
    print('{}x{}={}'.format(num1, num2, multiply_numbers(num1, num2)))

elif choice == 4:
    print('{}/{}={}'.format(num1, num2, divide_numbers(num1, num2)))

您的代码输出现在看起来像

 Enter 1 to add, 2 to subtract, 3 to multiply, 4 to divide. 1
What is your first number? 2
What is your second number? 4
yah
2+4=6

 Enter 1 to add, 2 to subtract, 3 to multiply, 4 to divide. 2
What is your first number? 8
What is your second number? 4
8-4=4

 Enter 1 to add, 2 to subtract, 3 to multiply, 4 to divide. 3
What is your first number? 4
What is your second number? 4
4x4=16

....

实际上,您可以使用dictionary来简化代码,这会将您的选择映射到一个函数,然后直接调用该函数

#Taking input as int
choice = int(input(" Enter 1 to add, 2 to subtract, 3 to multiply, 4 to divide. "))
num1 = int(input("What is your first number? "))
num2 = int(input("What is your second number? "))

def add_numbers(x,y):
    return x+y

def subtract_numbers(x,y):
    return x-y

def multiply_numbers(x,y):
    return x*y

def divide_numbers(x,y):
    return x/y

#Map of choice to operations
op_functions = {1: add_numbers, 2: subtract_numbers, 3: multiply_numbers, 4: divide_numbers}

#Map of choice to operator symbols
symbols = {1: '+', 2: '-', 3: '*', 4: '/'}

#Based on choice, choose corresponding operator and operation
print('{}{}{}={}'.format(num1, symbols[choice], num2, op_functions[choice](num1, num2)))