# My calculator code is not printing the answer

I have not tried anything since I am stuck

This program creates a simple calculator

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

This function subracts one number from another

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

This function multiplies two numbers together

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

This function divides one number by another

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

Take input from user

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

I want the answer to print but it will not On

• You are taking input as integer `choice = int(input(...))` but comparing to an string in `choice == '1'`, choose one, I would suggest integer to integer comparison
e.g. `choice == 1`

• You are trying to append integers and string, when you are trying to print e.g. `((num1) + "/" + (num2)` which won't work, I suggest using string formatting to print your output, str.format e.g. `print('{}+{}={}'.format(num1, num2, add_numbers(num1, num2)))`

So a sample fix of your code might look like

``````#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? "))

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

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

Output of your code now will look like

`````` 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

....
``````

Also you can actually simplify a code a lot by using a `dictionary`, which will map your choice to a function and then call the function directly

``````#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? "))

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