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

Python calculator

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

Problem

I'd appreciate any comments about my Python calculator, ways in which it can be improved and where it's currently lacking.

import operator

"""
Do the actual calculation
"""
def calculator(operation, operand_1, operand_2):
    ops = {"+": operator.add, "-": operator.sub, "/": operator.div, "*": operator.mul}
    return ops[operation](operand_1, operand_2)

"""
Initiate the user interface and input from user
"""
def main():
    print_line(19)
    print("WELCOME to CALC+-/*")
    print_line(19)
    while True:
        operation = raw_input("Operation type: ")
        operand_1 = int(raw_input("Operand 1: "))
        operand_2 = int(raw_input("Operand 2: "))
        calculation = calculator(operation, operand_1, operand_2)
        print("Result: " + str(calculation))
        if raw_input("Enter to continue, or 'q' to quit: "):
            break

"""
Utility function to print dashed lines
"""
def print_line(line_length):
    print("-"*line_length)

main()

Solution

The docstring of a function should be at the top of the inside of a function.

For example this:

"""
Utility function to print dashed lines
"""
def print_line(line_length):
    print("-"*line_length) # Also, space the operators out.


should be:

def print_line(line_length):
    """Utility function to print dashed lines"""
    print("-" * line_length)


Hmmm:

print_line(19)
print("WELCOME to CALC+-/*")
print_line(19)


Why 19? I would probably create a variable called header and get it's length:

header = "WELCOME to CALC+-/*"
print_line(len(header))
print(header)
print_line(len(header))


So you aren't confused why you used 19 later on when you forget.

You should also have two newlines between functions.

This conditional:

if raw_input("Enter to continue, or 'q' to quit: "):
    break


Allows me to exit if I type anything including (but not limited to) q.

cont = raw_input("Enter to continue, or 'q' to quit: ")
while True:
    if cont == "":
        break
    elif cont == "q":
        return
    cont = raw_input("Invalid command, please try again: ")


You also have some other fragile things, if you input + or any operand with spaces the program will crash.

replace the spaces with nothing:

operation = raw_input("Operation type: ").replace(" ", "")


Also, by convention you should do:

if __name__ == "__main__":
    main()


This answer illustrates some reasons why. Although I think it is unlikely this program will be imported by another program, you might as well be safe than sorry.

Code Snippets

"""
Utility function to print dashed lines
"""
def print_line(line_length):
    print("-"*line_length) # Also, space the operators out.
def print_line(line_length):
    """Utility function to print dashed lines"""
    print("-" * line_length)
print_line(19)
print("WELCOME to CALC+-/*")
print_line(19)
header = "WELCOME to CALC+-/*"
print_line(len(header))
print(header)
print_line(len(header))
if raw_input("Enter to continue, or 'q' to quit: "):
    break

Context

StackExchange Code Review Q#139749, answer score: 6

Revisions (0)

No revisions yet.