jane12
jane12

Reputation: 1

print function doesn't execute outside a while cycle

i have this function that works as intended except for the fact that the last print instruction outside the while cycle (print("why don't you print?")) never get executed and i don't understand why. after the break, the code execution should move forward.

def eval_cycle():
    done = 'done'
    last_expression = ' '
    while True:
        dato = eval(input('Insert an expression: '))
        if dato == done:
            print("Last expression is: ", last_expression)
            return dato
            break
        last_expression = dato
        print(dato)
    print("why don't you print?")

Upvotes: 0

Views: 36

Answers (1)

Chris
Chris

Reputation: 36596

When you return it immediately returns control flow out of the function.

Nothing in the function after that is executed.

As a sidenote, you may want to read the expression in as a string and then execute it. That way you can store it in last_expression rather than just the result of evaling the expression.

Upvotes: 1

Related Questions