# How would I make this more compact and faster? New learner

Sorry to ask a very long question but I am very new to Python. I started a free course on FutureLearn and a task was to create a calculator. The code I had ended up being a lot longer than other answers and it was too big to fit into the comment section. Just looking for tips for any part of my code that could be squished down. First question on StackOverflow so I'm sorry if this is a bad question.

``````def main():
operations = ['multiplication', 'division', 'addition', 'subtraction', 'x^y']

def find_op():
while True:
try:
operation = input('What do you want to do? Addition, subtraction, multiplication, division, or x^y??\n')
operations.index(operation.lower())
break
except ValueError:
input('Error!, you must input one of the five options, enter to try again.')
return operations.index(operation.lower())

operation_index = find_op()

match operation_index:
case 0:
num_question_ending = 'multiply with'
case 1:
num_question_ending = 'divide with'
case 2:
case 3:
num_question_ending = 'subtract with'
case 4:
num_question_ending = 'create an exponential with'

def sub():
sub_ans = global_number1 - global_number2
print(f'{global_number1} - {global_number2} =\n{sub_ans}')

def mult():
mult_ans = global_number1 * global_number2
print(f'{global_number1} multiplied by {global_number2} =\n{mult_ans}')

def div():
while True:
try:
div_ans = global_number1 / global_number2
break
except ZeroDivisionError:
input('Naughty naughty boy trying to divide by 0. '
'Now you gonna have to restart the code. Press enter plz')
main()
print(f'{global_number1} divided by {global_number2} =\n{div_ans}')

def power():
if global_number1 == 0 and global_number2 == 0:
input('Naughty boy trying 0^0, dat is undefined boi. Enter to restart the whole thing.')
main()
pow_ans = global_number1 ** global_number2
print(f'{global_number1} to the power of {global_number2} =\n{pow_ans}')

def get_number1():
while True:
try:
numba1 = input(f'what\'s the first number you want to {num_question_ending}??\n')
float(numba1)
break
except ValueError:
input('input must be a number. enter to try again.')

numba1 = float(numba1)
return numba1

def get_number2():
while True:
try:
numba2 = input(f'what\'s the second number you want to {num_question_ending}??\n')
float(numba2)
break
except ValueError:
input('input must be a number. enter to try again.')

numba2 = float(numba2)
return numba2

global_number1 = get_number1()

global_number2 = get_number2()

match operation_index:
case 0:
mult()
case 1:
div()
case 2:
case 3:
sub()
case 4:
power()

def repeat():
go_again_ans = input('would you like to go again? Y/N\n')
if go_again_ans == 'Y':
main()
elif go_again_ans == 'N':
exit()
else:
input('Error! You need to answer with either Y or N, enter to try again.')
repeat()
repeat()

if __name__ == '__main__':
main()
``````

I think this is the most concise way I used the menu prompt to save the text, first class functions to not search for the operation and a lambda functions to avoid defining each function in 3 lines, also not checking for the zero I only catch the exception

I am also still learning so excuse me if something is off

Thanks

``````menu_prompt = f"""
's' For Subtraction
'm' For Multiplication
'd' For Division
'p' For Exponential operations
'q' To quit
"""

add = lambda x, y: f"{x} + {y} = { x + y}"
subtract = lambda x, y: f"{x} - {y} = { x - y}"
multiply = lambda x, y: f"{x} * {y} = { x * y}"
divide = lambda x, y: f"{x} / {y} = { x / y:.2f}"
power = lambda x, y: f"{x} ^ {y} = { x ** y}"

operations = {
's': subtract,
'm': multiply,
'd': divide,
'p': power
}

def get_number(order):
while True:
try:
return float(input(f"Please enter the {order} number: "))

except ValueError:

def run_selected_operation(operation):
x = get_number("first")
y = get_number("second")
try:
print(operation(x, y))
except ZeroDivisionError:
print("Can not divide by zero, try again")