Calculator that can add/subtract/divide/multiply two inputs Here I am making a calculator for a robotics assignment. I need the program to be able to take two inputs and convert to float. It also needs to display previous calculations when exiting and also stop when trying to divide by 0. It works I just want to make sure its clean and im not using any bad practices.
operators = [&quot;+&quot;, &quot;-&quot;, &quot;*&quot;, &quot;/&quot;]
answers = []
def run_calculator():
    def instructions():
        try: chosen_operator = int(input(&quot;Choose an Operator:\n1) Addition\n2) Subtraction\n3) Multiplication\n4) Division\n\n5) Exit\nInput (1/2/3/4/5):&quot;))
        except:
            print('Invalid Input: Choose A Valid Operation')
            return
        if chosen_operator &gt; 4 or chosen_operator &lt; 1:
            if chosen_operator == 5: return
            print('Invalid Input: Choose a Valid Operation')
            return
        operator = operators[chosen_operator - 1]
        try: firstnum = float(input('First Number:'))
        except: 
            print('Invalid Input: Not a valid number')
            return
        try: secondnum = float(input('Second Number:'))
        except: 
            print('Invalid Input: Not a valid number')
            return
        return operator, firstnum, secondnum
    while (True):
        try: operator, firstnum, secondnum = instructions()
        except: break
        if operator == &quot;/&quot; and secondnum == 0.0:
            print(&quot;Cannot divide by 0&quot;)
            break
        calc_function = float.__add__ if operator == &quot;+&quot; else float.__sub__ if operator == &quot;-&quot; else float.__mul__ if operator == &quot;*&quot; else float.__truediv__
        answers.append(f&quot;{firstnum} {operator} {secondnum} = {calc_function(firstnum, secondnum)}&quot;)
        restart = input((f&quot;{firstnum} {operator} {secondnum} = {calc_function(firstnum, secondnum)}\nWould you like to do another calculation? (Y/N)&quot;))
        if restart.lower() == 'y':
            continue
        else:
            print(&quot;Results:&quot;)
            for x in range(len(answers)):
                print(f&quot;{answers[x]}&quot;)
            break
run_calculator()
Immutable sequences
operators should be a tuple (i.e. ('+', ...)), since you don't change it.
Nested functions
instructions should be moved to the global level - you shouldn't need a closure, and should be passing information between instructions and run_calculator with parameters and return values, not closure variables.
Index-based selection
It's a little awkward to ask that the user select an operator based on numerical index. Make operators a dictionary instead, and have the user input the operator character. Of course, this is only possible if the parameters of your homework assignment permit.
Never except: 
Seriously. except ValueError: instead. Otherwise, you'll catch exceptions you don't want to, including user-break Ctrl+C.
Parens
Python doesn't need parens here:
while (True):

so delete them.
Logic inversion
    if restart.lower() == 'y':
        continue
    else:
        print(&quot;Results:&quot;)
        for x in range(len(answers)):
            print(f&quot;{answers[x]}&quot;)
        break

is more easily expressed as
    if restart.lower() != 'y':
        print(&quot;Results:&quot;)
        for x in range(len(answers)):
            print(f&quot;{answers[x]}&quot;)
        break