I'm programming a beginner calculator in Python.
I'm stuck trying to rerun the function automatically after getting the return value.
So far it reruns when the except block is triggered.
However, it does not rerun when a sum is entered correctly.
def better_calc():
num1 = float(input("please enter your first number: "))
op = input("please enter an operator: ")
num2 = float(input("please enter your second number: "))
try:
if op == "+":
result = num1+num2
elif op == "-":
result = num1-num2
elif op == "*":
result = num1*num2
elif op == "/":
result = num1/num2
print()
print(num1, op, num2, "=")
return result
better_calc()
print()
except UnboundLocalError:
print("\nError: please enter an established operator")
print()
except ZeroDivisionError:
print("\nError: Can not divide by zero")
print()
better_calc()
print(better_calc())
So I have two questions.
(1) How do I rerun the function after getting the return value?
(2) Should I bother trying to get a return value (is there any benefit?), or just print the answer without a return?