Sea Wolf
Sea Wolf

Reputation: 81

Python basic Calculator program doesn't return answer

So I am trying to figure out how to make a calculator with the things that I have learned in python, but I just can't make it give me an answer.

This is the code I have so far:

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

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


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


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


print("What calculation would you like to make?")
print("Add")
print("Subtract")
print("Divide")
print("Multiply")


choice = input("Enter choice (add/subtract/divide/multiply)\n")


num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))        

if choice == ("add, Add"):
    print(add(num1,num2))

elif choice == ("subtract, Subtract"):
    print(subtract(num1,num2))

elif choice == ("divide, Divide"):
    print(divide(num1,num2))

elif choice == ("multiply, Multiply"):
    print(multiply(num1,num2))`

Upvotes: 4

Views: 162

Answers (2)

FEARSIlent15
FEARSIlent15

Reputation: 1

I have a very basic calc. you can try so that you can learn from it. while True:

firstNum = int(input('first number'))
op = input('enter operation')
secondNum = int(input('second number'))

if op == '-':
    print(firstNum - secondNum)
elif op == '+':
    print(firstNum + secondNum)
elif op == '*':
    print(firstNum * secondNum)
elif op == '/':
    print(firstNum / secondNum)
else:
    print('ERROR, You must enter an operation.')

Upvotes: 0

gnicholas
gnicholas

Reputation: 2077

Instead of:

choice == ("add, Add")

You want:

choice in ["add", "Add"]

Or more likely:

choice.lower() == "add"

Why? You're trying to check that the choice input is equal to the tuple ("add, Add") in your code which is not what you want. You instead want to check that the choice input is in the list ["add", "Add"]. Alternatively the better way to handle this input would be to lowercase the input and compare it to the string you want.

Upvotes: 8

Related Questions