Using numbers to encode user input isn't necessarily bad, but your code shouldn't have those repeated throughout it. Consider instead using an Enum, which ships with Python 3, and is pip install
-able in Python >=2.4; http://stackoverflow.com/a/1695250/2581168 https://stackoverflow.com/a/1695250/2581168.
Using numbers to encode user input isn't necessarily bad, but your code shouldn't have those repeated throughout it. Consider instead using an Enum, which ships with Python 3, and is pip install
-able in Python >=2.4; http://stackoverflow.com/a/1695250/2581168.
Using numbers to encode user input isn't necessarily bad, but your code shouldn't have those repeated throughout it. Consider instead using an Enum, which ships with Python 3, and is pip install
-able in Python >=2.4; https://stackoverflow.com/a/1695250/2581168.
General Notes
Regarding PyCALC:Write more functions! You're copy-pasting a lot of stuff around here. If you copy paste anything at all, you should stop for a moment and ask yourself "why can't I make this a function"? Prompting the user for a filename and returning that filename on the end of a path? That's a function. Prompting the user for the contents of a file and replacing its contents with that? That's also a function.
Use modules! https://docs.python.org/2/tutorial/modules.html . Your calculator shouldn't be in the same file as your text editor, you'll just confuse yourself when they start to expand.
Regarding PyCALC:
from enum import IntEnum # run 'pip install enum' first if you're not on Python 3!
class Operation(IntEnum):
add = 0
sub = 1
mul = 2
div = 3
def perform_calculation(op):
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
print ("Please wait...")
time.sleep(0.6)
if op == Choice.add:
answer = num1+num2
elif op == Choice.sub:
answer = num1-num2
elif op == Choice.mul:
answer = num1*num2
elif op == Choice.div:
answer = num1/num2
print ("Your answer is:")
print (answer)
break
def calc():
valid_choice = False
while not valid_choice:
print ("Welcome to PyCALC")
print ("Please type the number for your suggestion.")
print ("{0}: Addition\n{1}: Subtraction\n{2}: Multiplacation\n{3}: Division".format(int(Choice.add), int(Choice.sub), int(Choice.mul), int(Choice.div)))
input_op = int(input("Your Choice: "))
if input_op.isdigit() and int(input_op) in Operation.__members__.values():
perform_calculation(input_op)
valid_choice = True
else:
print ("Your operation choice is invalid!")
Regarding PyCALC:
from enum import IntEnum # run 'pip install enum' first if you're not on Python 3!
class Operation(IntEnum):
add = 0
sub = 1
mul = 2
div = 3
def perform_calculation(op):
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
print ("Please wait...")
time.sleep(0.6)
if op == Choice.add:
answer = num1+num2
elif op == Choice.sub:
answer = num1-num2
elif op == Choice.mul:
answer = num1*num2
elif op == Choice.div:
answer = num1/num2
print ("Your answer is:")
print (answer)
break
def calc():
valid_choice = False
while not valid_choice:
print ("Welcome to PyCALC")
print ("Please type the number for your suggestion.")
print ("{0}: Addition\n{1}: Subtraction\n{2}: Multiplacation\n{3}: Division".format(int(Choice.add), int(Choice.sub), int(Choice.mul), int(Choice.div)))
input_op = int(input("Your Choice: "))
if input_op in Operation.__members__.values():
perform_calculation(input_op)
valid_choice = True
else:
print ("Your operation choice is invalid!")
General Notes
Write more functions! You're copy-pasting a lot of stuff around here. If you copy paste anything at all, you should stop for a moment and ask yourself "why can't I make this a function"? Prompting the user for a filename and returning that filename on the end of a path? That's a function. Prompting the user for the contents of a file and replacing its contents with that? That's also a function.
Use modules! https://docs.python.org/2/tutorial/modules.html . Your calculator shouldn't be in the same file as your text editor, you'll just confuse yourself when they start to expand.
Regarding PyCALC:
from enum import IntEnum # run 'pip install enum' first if you're not on Python 3!
class Operation(IntEnum):
add = 0
sub = 1
mul = 2
div = 3
def perform_calculation(op):
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
print ("Please wait...")
time.sleep(0.6)
if op == Choice.add:
answer = num1+num2
elif op == Choice.sub:
answer = num1-num2
elif op == Choice.mul:
answer = num1*num2
elif op == Choice.div:
answer = num1/num2
print ("Your answer is:")
print (answer)
break
def calc():
valid_choice = False
while not valid_choice:
print ("Welcome to PyCALC")
print ("Please type the number for your suggestion.")
print ("{0}: Addition\n{1}: Subtraction\n{2}: Multiplacation\n{3}: Division".format(int(Choice.add), int(Choice.sub), int(Choice.mul), int(Choice.div)))
input_op = input("Your Choice: ")
if input_op.isdigit() and int(input_op) in Operation.__members__.values():
perform_calculation(input_op)
valid_choice = True
else:
print ("Your operation choice is invalid!")
Regarding PyCALC:
def calc():
while True:
print ("Welcome to PyCALC")
print ("Please type the number for your suggestion.")
print ("1: Addition\n2: Subtraction\n3: Multiplacation\n4: Division")
suggestion = input("Your Choice: ")
if suggestion == "1":
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
print ("Please wait...")
time.sleep(0.6)
answer = num1+num2
print ("Your answer is:")
print (answer)
break
if suggestion == "2":
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
print ("Please wait...")
time.sleep(0.6)
answer = num1-num2
print ("Your answer is:")
print (answer)
break
if suggestion == "3":
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
print ("Please wait...")
time.sleep(0.6)
answer = num1*num2
print ("Your answer is:")
print (answer)
break
if suggestion == "4":
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
print ("Please wait...")
time.sleep(0.6)
answer = num1/num2
print ("Your answer is:")
print (answer)
break
else:
print ("Your operation choice is invalid!")
Using numbers to encode user input isn't necessarily bad, but your code shouldn't have those repeated throughout it. Consider instead using an Enum, which ships with Python 3, and is pip install
-able in Python >=2.4; http://stackoverflow.com/a/1695250/2581168.
The conditions under which this function is exited aren't clear. Consider using an explicit valid_input
parameter, initialised to False
, which is set to True
when the user inputs something valid.
If the user inputs something valid, all four outcomes are almost completely identical. Consider putting them all into another function, where only that one line is branched.
from enum import IntEnum # run 'pip install enum' first if you're not on Python 3!
class Operation(IntEnum):
add = 0
sub = 1
mul = 2
div = 3
def perform_calculation(op):
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
print ("Please wait...")
time.sleep(0.6)
if op == Choice.add:
answer = num1+num2
elif op == Choice.sub:
answer = num1-num2
elif op == Choice.mul:
answer = num1*num2
elif op == Choice.div:
answer = num1/num2
print ("Your answer is:")
print (answer)
break
def calc():
valid_choice = False
while not valid_choice:
print ("Welcome to PyCALC")
print ("Please type the number for your suggestion.")
print ("{0}: Addition\n{1}: Subtraction\n{2}: Multiplacation\n{3}: Division".format(int(Choice.add), int(Choice.sub), int(Choice.mul), int(Choice.div)))
input_op = int(input("Your Choice: "))
if input_op in Operation.__members__.values():
perform_calculation(input_op)
valid_choice = True
else:
print ("Your operation choice is invalid!")
It's possibly a bit out of scope, but if you wanted to win brownie points from functional programmers, take a look at the Operator library and see if you can find an even cleaner way of doing this; https://docs.python.org/2/library/operator.html.