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))`
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With