This is my second Python beginner program done without help. I would like to receive comments on how clean does it look and if it can be done more efficiently.
def user_operation():
return input('Type operation: ')
def user_number():
"""Return numbers used in operations"""
n1 = input('First number: ')
n2 = input('Second number: ')
return (int(n1), int(n2))
#Operations
def add(x, y):
a = x + y
print ('%d + %d is %d' % (x, y, a))
def sub(x, y):
s = x - y
print ('%d - %d = %d' % (x, y, s))
def mult(x, y):
m = x * y
print('%d * %d is %d' % (x, y, m))
def div(x, y):
d = x / y
print ('%d / %d is %d' % (x, y, d))
def calculate():
while True:
n1, n2 = user_number()
operation = user_operation()
if operation.lower() == 'quit':
return False
elif operation == '1':
add(n1, n2)
elif operation == '2':
sub(n1, n2)
elif operation == '3':
mult(n1, n2)
elif operation == '4':
div(n1, n2)
else:
print ("That is not an operation!!")
def main():
print ("Calculator program by Jfelix\n"
"Available operations:\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n"
"Type their respective number to perform the selected operation\n"
"If done with an operation, type 'back'\n"
"Type 'quit' to end the program.\n"
)
calculate()
if __name__ == "__main__":
main()