I am extremely new, I am learning on my own. I am creating my first real project, one function of which is a calculator.
I am requesting 2 numbers and an operator from the user. I have some history in Pascal and Delphi from high school so the concept of validating my data before using it is not missed on me.
I have used an if != statement to check my operator is valid before proceeding. It will always run that argument as true and return my error message of invalid operator. I have tested by removing all of my “or” checks and just checking if the op is “+”, this seems to work correctly each time. i feel I am misunderstanding the use of or within the if statements.
What am I misunderstanding here?
def calc(): num1 = float(input("Enter first number: ")) op = input("Enter operator[+, -, *, /]: ") num2 = float(input("Enter first number: ")) while True: try: print(num1, op, num2) if op != "+" or "-" or "*" or "/": print("Invalid operator, please try again") print(op) calc() elif op == "+": answer = (num1 + num2) elif op == "-": answer = (num1 - num2) elif op == "*": answer = (num1 * num2) elif op == "/": if num2 != 0: answer = (num1 / num2) print("You can not divide by 0. Please try again") calc() print(answer) break except ValueError: print("You have entered invalid numbers, please try again.") calc()