6
\$\begingroup\$

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()
asked May 15, 2017 at 2:35
\$\endgroup\$
3
  • 3
    \$\begingroup\$ 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 :-) \$\endgroup\$ Commented May 15, 2017 at 7:07
  • \$\begingroup\$ That is a typo. @ChatterOne \$\endgroup\$ Commented May 15, 2017 at 8:37
  • 3
    \$\begingroup\$ Since you have not received an answer yet, you can still fix it, though. \$\endgroup\$ Commented May 15, 2017 at 15:36

2 Answers 2

4
\$\begingroup\$

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
def add(x, y):
 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 == '+':
 print (add(n1, n2))
 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()
answered May 15, 2017 at 21:42
\$\endgroup\$
0
2
\$\begingroup\$

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))
...
func = {'+': add,
 '-': 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.

answered May 16, 2017 at 10:18
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.