0

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?

Prateek Dewan
  • 1,587
  • 3
  • 16
  • 29
Aaron Daly
  • 27
  • 4
  • 2
    Run it in a loop? – Jan Christoph Terasa May 30 '20 at 11:22
  • 1
    Anything below the `return` is not executed. `return` *ends* a function, it does not "set the return value". – Jongware May 30 '20 at 11:54
  • Use ```while True``` and break it by asking the user to continue using your calculator. Also there is no use of unnecessary ```print()```, i guess you want to leave a empty line, you can use ```\n``` in print. – JenilDave May 30 '20 at 11:58
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – quamrana May 30 '20 at 12:20

1 Answers1

0

As someone stated in the comments, anything below a return statement is not run because it ends the function, bearing this in mind, my answer to your second question would be no, there is no benefit (in this case) to getting a return value. Instead, I would change your function to this:

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(str(num1)+" "+ str(op)+" "+ str(num2)+ " = " + str(result)) 
    #You just print the result of the calculation above
    except UnboundLocalError:
        print("\nError: please enter an established operator")
        print()
    except ZeroDivisionError:
        print("\nError: Can not divide by zero")
        print()
    answer = input("Would you like to enter another calculation?(type \"yes\" if so): ")
    return answer
    #You are using this return statement to either continue or end the while loop

I would also recommend encasing your file in a while loop like so:

#start of file
#have your better_calc function definition here
startingAnswer = "yes"

while(startingAnswer = "yes"):
    startingAnswer = better_calc()
#end of file

This way, you are able to continue doing calculations as long as you want (while not running into infinite loop issues).