2025, Sep 26 21:00
Why your interactive Python calculator restarts without showing a result (and how to fix it)
Learn why an interactive Python calculator restarts without output: returning instead of printing, bad input checks, and the exact code fix to show results reliably. Fix included.
Interactive Python calculator keeps restarting and shows no result: what’s happening and how to fix it
Building a small interactive calculator is a great way to learn Python’s I/O and control flow. But a common stumbling block is expecting a function to show a result just because it returns a value. If your program accepts all inputs and then restarts without displaying the final answer, you’re most likely returning the result instead of printing it. There are also a few control-flow and formatting issues that can hide the intended behavior.
Problematic example
The following code captures the issue. It reads numbers and an operation, formats a vertical arithmetic view, and then exits prematurely before anything is shown to the console.
def calc_runner():
    
    while True:
        try:
            lhs = float(input('Add 1st number: '))
            rhs = float(input('Add 2nd number: '))
        except ValueError:
            print('Invalid input. Please enter a number')
            continue
        
        ops_set = set(['+', '-', '/', '*'])
        try:
            op_choice = input('Please choose operation from [+, -, *, /]: ')
        except op_choice not in ops_set:
            print(f'Function must contain mathetmatical operation values only')
            continue
                
        
        header_line = ""
        op_line = ""
        dash_line = ""
        out_line = ""
        if op_choice == '+':
            result_val = lhs + rhs
        elif op_choice == '-':
            result_val = lhs - rhs
        elif op_choice == '*':
            result_val = lhs * rhs
        elif op_choice == '/':
            if rhs == 0:
                print(f'Division by 0 not possible')
                continue
            else:
                result_val = lhs / rhs
            
        s_lhs = str(lhs)
        s_rhs = str(rhs)
        s_out = str(result_val)
        col_width = max(len(s_lhs), len(s_rhs)) + 2
        top_line = s_lhs.rjust(col_width)
        bottom_line = op_choice + s_rhs.rjust(col_width - 1)
        sep_line = '-' * col_width
        res_line = s_out.rjust(col_width)
        header_line += top_line
        op_line += bottom_line
        dash_line += sep_line
        out_line += res_line
        display_block = f'{header_line}\n{op_line}\n{dash_line}\n{out_line}'
        return display_block
        exit_choice = input('Quit Program? Y/N: ')
        if exit_choice == 'Y' or exit_choice == 'y':
            break
calc_runner()
Why the program doesn’t show the result
The core issue is the use of return instead of print for the final output. The return statement hands a value back to the caller and immediately exits the function; it does not display anything to the terminal. Because the function returns before any printing or exit prompt, nothing is shown and the code after return never runs.
There is a second logic problem in the operation check. The except block is used where a regular conditional check is needed. The except clause only runs when an exception is raised, not when a value fails a membership test. To validate input against a set of allowed symbols, use an if condition and continue the loop when the input is invalid.
Several formatting variables are redundantly built using += even though they are never accumulating multiple values. This obscures the intent and adds noise. It’s clearer to compute the formatted strings once and use them directly.
There is also a minor control flow smell after guarding against division by zero. Once continue is executed, code following that branch won’t run, so an else is unnecessary.
Finally, the quit prompt compares two cases explicitly. Normalizing the input with lower() makes the condition simpler without changing the behavior.
Fix and improved version
The corrected version prints the computed block, validates the operation with an if check, removes redundant assignments, simplifies the division branch, and streamlines the quit logic. The overall behavior remains the same, but the output is now shown properly.
def calc_runner():
    while True:
        try:
            lhs = float(input('Add 1st number: '))
            rhs = float(input('Add 2nd number: '))
        except ValueError:
            print('Invalid input. Please enter a number')
            continue
        ops_set = {'+', '-', '/', '*'}
        op_choice = input('Please choose operation from [+, -, *, /]: ')
        if op_choice not in ops_set:
            print(f'Function must contain mathematical operation values only')
            continue
        if op_choice == '+':
            result_val = lhs + rhs
        elif op_choice == '-':
            result_val = lhs - rhs
        elif op_choice == '*':
            result_val = lhs * rhs
        elif op_choice == '/':
            if rhs == 0:
                print(f'Division by 0 not possible')
                continue
            result_val = lhs / rhs
        s_lhs = str(lhs)
        s_rhs = str(rhs)
        s_out = str(result_val)
        col_width = max(len(s_lhs), len(s_rhs)) + 2
        top_line = s_lhs.rjust(col_width)
        bottom_line = op_choice + s_rhs.rjust(col_width - 1)
        sep_line = '-' * col_width
        res_line = s_out.rjust(col_width)
        display_block = f'{top_line}\n{bottom_line}\n{sep_line}\n{res_line}'
        print(display_block)
        exit_choice = input('Quit Program? Y/N: ')
        if exit_choice.lower() == 'y':
            break
calc_runner()
Why it’s worth knowing
Understanding the distinction between returning data and printing to stdout is fundamental in Python. It affects how you structure functions, how you test them, and how you present results in CLI tools. Clear input validation prevents confusing control flow, while straightforward string formatting and simplified branches make the code easier to read, debug, and extend. Small improvements like normalizing user input also reduce duplication and subtle bugs.
Takeaways and practical advice
If you want to display something in a terminal session, use print at the point where the user expects feedback. Reserve return for passing values between functions. Validate user input with regular conditionals, not exception handlers, unless you’re genuinely catching errors like ValueError from float conversion. Avoid redundant variables and unnecessary else blocks after continue. Keep your loop exit logic predictable by normalizing user responses before comparison.