Review
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)
You import the whole module, when, in fact, you just need four of its functions. Result: you eat too much RAM.
Try doing dir(__import__('operator'))
. You will be really baffled because it has 126 functions (aside from the built-in module ones), while you need 4.
When having to work with lots of data, even the last byte of your RAM must be saved, so import less, eat less! It's a good practice to import less.
Here is a solution to the problem:
from operator import add, sub, div, mul
"""
Do the actual calculation
"""
def calculator(operation, operand_1, operand_2):
ops = {"+": add, "-": sub, "/": div, "*": mul}
return ops[operation](operand_1, operand_2)
"""
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)
I can see these are not docstrings, as @Dair believes; they are comments, because the main function also has one over it. Please use single-line comments instead (#comment
):
#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)
print("-"*line_length)
Here, you seem to have missed the spaces. Here's the fix:
print("-" * line_length)
print("WELCOME to CALC+-/*")
print("Result: " + str(calculation))
print("-"*line_length)
It's not python-3.x here, so print
is a statement, not a function!
It's not normal to refer to literals and whole expressions like (a)
, and it uselessly slows down code, especially with lots of data (i.e. [(i) for i in xrange(2562700000)]
), so here is a fix:
print "WELCOME to CALC+-/*"
print "Result: " + str(calculation)
print "-"*line_length
main()
You seem not to call the main function correctly. Here is how you should call it, because, otherwise, the program might get imported and run:
if __name__ == "__main__":
main()
Some variable names are really large, others are too short/not clear enough.
if raw_input("Enter to continue, or 'q' to quit: "):
break
You would allow any input other than
to exit your program here, not just q
. The solution is here:
if raw_input("Enter to continue, or 'q' to quit: ") == "q":
break
I would also suggest supporting both Q
and q
, like this:
if raw_input("Enter to continue, or 'q' to quit: ").lower() == "q":
break
print("Result: " + str(calculation))
There is string formatting for that!
print("Result: %d" % calculation)
print_line(19)
print("WELCOME to CALC+-/*")
print_line(19)
Whoa, wait a sec, what!? The len
builtin can help you here!
header = "WELCOME to CALC+-/*"
print_line(len(header))
print(header)
print_line(len(header))
Result
from operator import add, sub, div, mul
#Do the actual calculation
def calc(op, a, b):
ops = {"+": add, "-": sub, "/": div, "*": mul}
return ops[op](a, b)
#Initiate the user interface and input from user
def main():
header = "WELCOME to CALC+-/*"
print_dashed_line(len(header))
print header
print_dashed_line(len(header))
while True:
op = raw_input("Operation type: ")
a = int(raw_input("Operand 1: "))
b = int(raw_input("Operand 2: "))
r = calc(op, a, b)
print "Result: %d" % r
if raw_input("Enter to continue, or 'q' to quit: ").lower() == "q":
break
#Utility function to print dashed lines
def print_dashed_line(length):
print "-" * length
if __name__ == "__main__":
main()