# Python 3 - Calculator

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
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':
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"
"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()

• The division operation is missing its arguments, which means that technically the code is not ready for review (because the code is not working)... But since you're a beginner that's showing some effort, I'm sure a review is coming anyway :-) Commented May 15, 2017 at 7:07
• That is a typo. @ChatterOne Commented May 15, 2017 at 8:37
• Since you have not received an answer yet, you can still fix it, though. Commented May 15, 2017 at 15:36

Nice first project! It is generally better practice to have functions return values instead of printing them. I added a power function to yours and made it more user friendly by having them type the symbol instead of a number.also you should add square, cube and other roots making the symbol '//'. otherwise it looks good, your code is nicely formatted and looks great.

def user_operation():
return input('Type operation: ')

def user_number():
#Return numbers used in operations
n1 = int(input('First number: '))
n2 = int(input('Second number: '))
return (n1, n2)

#Operations
a = x + y
return ('%d + %d is %d' % (x, y, a))
def sub(x, y):
s = x - y
return ('%d - %d = %d' % (x, y, s))
def mult(x, y):
m = x * y
return ('%d * %d is %d' % (x, y, m))
def div(x, y):
d = x / y
return ('%d / %d is %d' % (x, y, d))
def sqr(x,y):
d = x ** y
return ('%d ** %d is %d' % (x, y, d))
def calculate():
while True:
n1, n2 = user_number()
operation = user_operation()
if operation.lower() == 'quit':
break
elif operation == '+':
elif operation == '-':
print (sub(n1, n2))
elif operation == '*':
print (mult(n1, n2))
elif operation == '/':
print (div(n1, n2))
elif operation == '**':
print (sqr(n1, n2))
else:
print ("That is not a valid operation!!")

def main():
print ("Calculator program by Jfelix\n"
"Available operations:\n1. Addition (+)\n2. Subtraction (-)\n3. Multiplication(*)\n4. Division (/)\n5. Power (**)"
"Type their respective character(s) to perform the selected operation\n"
"Type 'quit' to end the program.\n")

calculate()

if __name__ == "__main__":
main()


Starting from @theman25's answer, you could define a dictionary that maps an input to the function to use. This works, because all your functions take two arguments (i.e. you have only binary and not unary functions):

def pow_(x, y):
d = x ** y
return ('%d ** %d is %d' % (x, y, d))
...

'-': sub,
'*': mult,
'/': div,
'**': pow_}

def calculate():
while True:
n1, n2 = user_number()
operation = user_operation()
if operation.lower() == 'quit':
break
try:
print (func[operation](n1, n2))
except KeyError:
print ("That is not a valid operation!!")


Note that I renamed his sqr function to pow_, because the built-in function for this is called pow.