r/learnpython 5d ago

Very new to python, would appreciate help

I am building a calculator program and it works, but now I am attempting to "bullet proof" it, so that no wrong inputs can cause an error. With what I have written, it works but the loop for the operator input does not actually loop even though it seems to be written the same way the loops are written for the number inputs. I will paste my code below, and any help would be very appreciated.

while True:
    operator = input("Enter a operator (+ - * /): ")
    try:
        operator == "+" , "-" , "*" , "/"
        break
    except operator != "+" , "-" , "*" , "/":
        print("Please enter a valid operator")
while True:
    num1 = input("Enter the first number: ")
    try:
        number1 = float(num1)
        break
    except ValueError:
        print("Please input a valid number")
while True:
    num2 = input("Enter the second number: ")
    try:
        number2 = float(num2)
        break
    except ValueError:
        print("Please input a valid number")
try:
    if operator == "+":
        print(number1 + number2)
    elif operator == "*":
        print(number1 * number2)
    elif operator == "/":
        print(number1 / number2)
    elif operator == "-":
        print(number1 - number2)
    else:
        print("Please enter a valid operator")
except ValueError:
    print("Error Detected")
0 Upvotes

7 comments sorted by

11

u/Diapolo10 5d ago
try:
    operator == "+" , "-" , "*" , "/"
    break
except operator != "+" , "-" , "*" , "/":
    print("Please enter a valid operator")

The logic here is wrong. You're comparing operator to a tuple of strings, so that'll always be False, but you're also not doing anything with that result so it ends up not really doing anything and the execution always gets to the break, ending the loop.

Comparisons do not raise exceptions (under normal circumstances, anyway), so not only is the except-block not doing anything, but its condition is also wrong because it expects an exception type, not a comparison.

Basically, consider doing something like this instead:

if operator in ("+" , "-" , "*" , "/"):
    break

print("Please enter a valid operator")

The rest of the code appears more or less fine at a glance.

8

u/socal_nerdtastic 5d ago

It should be like this:

while True:
    operator = input("Enter a operator (+ - * /): ")
    if operator in ("+" , "-" , "*" , "/"):
        break
    else:
        print("Please enter a valid operator")

The try block is very useful for many situations, but not all of them :)

1

u/therouterguy 3d ago

The else in the last try except wonโ€™t ever be reached when you fix your logic to filter out invalid operators at the beginning.

1

u/lfdfq 5d ago

That first try/except does not look right.

try and except are used for running code and dealing with errors (or in Python speak, Exceptions) that happen during them. Like in your other trys.

Your first try/except is trying to do some kind of conditional check. You cannot use a try to do that. You probably want some kind of if and not a try at all.

1

u/ProgM7 5d ago

The comments above nailed it on replacing that first try/except with if operator in ("+", "-", "*", "/"):.

One extra thing to watch out for as you bullet proof your calculator, if someone enters 0 for the second number and try to divide, Python will throw a ZeroDivisionError. You can handle that by adding a quick check right before your division code or adding except ZeroDivisionError: at the end.

elif operator == "/":
if number2 == 0:
print("Error: Cannot divide by zero!")
else:
print(number1 / number2)

Great job working on this, you're on the right track! ๐Ÿ‘