HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Python 3 - Calculator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
calculatorpythonstackoverflow

Problem

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()

Solution

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()

Code Snippets

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()

Context

StackExchange Code Review Q#163361, answer score: 4

Revisions (0)

No revisions yet.