# python – 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 = ("+", "-", "*", "/")
def run_calculator():
def instructions():
try: chosen_operator = int(input("Choose an Operator:n1) Additionn2) Subtractionn3) Multiplicationn4) Divisionnn5) ExitnInput (1/2/3/4/5):"))
except:
print('Invalid Input: Choose A Valid Operation')
return
if chosen_operator > 4 or chosen_operator < 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 == "/" and secondnum == 0.0:
print("Cannot divide by 0")
break
calc_function = float.__add__ if operator == "+" else float.__sub__ if operator == "-" else float.__mul__ if operator == "*" else float.__truediv__
answers.append(f"{firstnum} {operator} {secondnum} = {calc_function(firstnum, secondnum)}")
restart = input((f"{firstnum} {operator} {secondnum} = {calc_function(firstnum, secondnum)}nWould you like to do another calculation? (Y/N)"))
if restart.lower() == 'y':
continue
else:
print("Results:")