1

I'm new to python and am trying to make a basic calculator that takes two numbers and performs the operation specified by the user, but regardless of the operation I enter, the numbers always add.

num1 = int(input("Enter a number. "))
print(" ")
num2 = int(input("Enter another number. "))
print(" ")
operation = input("Type Add, Subtract, Multiply, or Divide" )

if operation == "Add" or "add":
    add = num1 + num2
    print(add)
elif operation == "Subtract" or "subtract":
    sub = num1 - num2
    print(sub)
elif operation == "Multiply" or "multiply":
    mult = num1 * num2
    print(mult)
elif operation == "Divide" or "divide":
    div = num1 / num2
    print(div)
else:
    print("Enter a valid operation: ")

With what I have, if num1 = 10 and num2 = 5 and I enter "multiply" the result I get is 15, not 50.

Matt
  • 15
  • 3

3 Answers3

1
if operation == "Add" or "add"

These are two conditions:

  1. if operation == "Add" which does what you expect.
  2. if "add" - Always evaluates to True because it's a constant string. So you could just as well have written: if True. You need to replace it with:
if operation == "Add" or operation == "add"

Anyway, I would like to suggest a few things.

Lowercase then check only once

You should lowercase your input string instead of double checking "Add" or "add":

num1 = int(input("Enter a number. "))
print(" ")
num2 = int(input("Enter another number. "))
print(" ")
operation = input("Type Add, Subtract, Multiply, or Divide" ).lower()  # Notice the `lower` here

if operation == "add":
    add = num1 + num2
    print(add)
elif operation == "subtract":
    sub = num1 - num2
    print(sub)
elif operation "multiply":
    mult = num1 * num2
    print(mult)
elif operation "divide":
    div = num1 / num2
    print(div)
else:
    print("Enter a valid operation: ")

Use the built-in operator module

The operator module contains exactly the methods that you seek for. You could use it for convenience (haven't tested, should work):


import operator
keywords = {"add": "add", "subtract": "sub", "multiply": "mul", "divide": "truediv"}

num1 = int(input("Enter a number. "))
print(" ")
num2 = int(input("Enter another number. "))
print(" ")
operation = input("Type Add, Subtract, Multiply, or Divide" )
op_func = keywords.get(operation.lower(), "add")  # defaults to add
print(getattr(operator, op_func)(num1, num2))
AdamGold
  • 4,941
  • 4
  • 29
  • 47
0
def fun(operation, num1,num2):
    operation = operation.lower()

    if operation == "add":
        add = num1 + num2
        print(add)
    elif operation == "subtract":
        sub = num1 - num2
        print(sub)
    elif operation == "multiply":
        mult = num1 * num2
        print(mult)
    elif operation == "divide":
        div = num1 / num2
        print(div)
    else:
        print("Enter a valid operation: ")

fun('multiply', 10,5)

Why:
if operation == "add" or "Add": this is always True
We can use if operation == "add" or operation == "Add":

Wang Liang
  • 4,244
  • 6
  • 22
  • 45
0

You have to do like this:

num1 = int(input("Enter a number. ")) 
print(" ") 
num2 = int(input("Enter another number. ")) 
print(" ") 
operation = str(input("Type Add, Subtract, Multiply, or Divide" ))
if operation == "Add" or operation =="add": 
    add = num1 + num2 
    print(add) 
elif operation == "Subtract" or operation =="subtract": 
    sub = num1 - num2 
    print(sub) 
elif operation == "Multiply" or operation =="multiply":
    mult = num1 * num2 
    print(mult) 
elif operation == "Divide" or operation =="divide": 
    div = num1 / num2 
    print(div) 
else: 
    print("Enter a valid operation: ")

Because of or operation with string, all your conditions will be true. That's why it will only go to first condition no matter what you give in the input.

Harshil Shah
  • 142
  • 2
  • 3
  • 13