-2

I have made a simple 4 function calculator app as practice

import time
import os
import subprocess
import sys

print("This is a calculator")
time.sleep(1)

num1 = float(input("Write a number of your choice: "))
op = input("Choose an operator: + - * / (or you can write their names)")
num2 = float(input("Write another number of your choice: "))
result = None


def cal(num1, op, num2, result):
    if op == "+" or "add" or "plus":
        result = num1 + num2
        return result
    elif op == "*" or "multiply" or "star" or "x":
        result = num1 * num2
        return result
    elif op == "/" or "divide" or "slash":
        result = num1 / num2
        return result
    elif op == "-" or "subtract" or "minus":
        result = num1 - num2
        return result
    else:
        print("Wrong input")
        subprocess.call(
            [sys.executable, os.path.realpath(__file__)] + sys.argv[1:])


print(cal(num1, op, num2, result))

I have encountered a problem, the calculator won't do any operation but adding as seen in the following example

This is a calculator
Write a number of your choice: 9
Choose an operator: + - * / (or you can write their names)/
Write another number of your choice: 7
16.0
  • `elif op == "*" or "multiply" or "star" or "x":` Classic ! I used to do this all the time. That code needs to reference op each time. `elif op == "*" or op == "multiply" or op == "star" or op == "x":` Like when you are doing If AND OR logic you need to do op == "*" or op == "+". With each statement in the or you have to be like op == this or op == that. It doesn't assume op is what your talking about if you don't reference it. So when you so op == "start" or op, its checking start but also it always will return true because `or op` is checking if op exists or not – taylorSeries Jun 10 '22 at 21:08
  • In short `or` and `and` compare two conditions. `op=="+"` is a condition, so all good there. `"add"` however is not a condition, it's just a string literal that `and` will consider `True` (since it isn't a literal `False` or `0`). – JNevill Jun 10 '22 at 21:13

1 Answers1

0

You can try this. It will work. The syntax you entered is wrong. You can also specify as op is "+" or op is "add". Something like this.

import time
import os
import subprocess
import sys

print("This is a calculator")
time.sleep(1)

num1 = float(input("Write a number of your choice: "))
op = input("Choose an operator: + - * / (or you can write their names)")
num2 = float(input("Write another number of your choice: "))
result = None


def cal(num1, op, num2, result):
    if op in ("+", "add", "plus"):
        result = num1 + num2
        return result
    elif op in ("*", "multiply", "star", "x"):
        result = num1 * num2
        return result
    elif op in ("/", "divide", "slash"):
        result = num1 / num2
        return result
    elif op in ("-", "subtract", "minus"):
        result = num1 - num2
        return result
    else:
        print("Wrong input")
        subprocess.call(
            [sys.executable, os.path.realpath(__file__)] + sys.argv[1:])


print(cal(num1, op, num2, result))