Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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))`
like image 299
Sea Wolf Avatar asked Feb 08 '23 03:02

Sea Wolf


1 Answers

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.

like image 151
gnicholas Avatar answered Feb 20 '23 02:02

gnicholas