I made a post a couple of days ago asking if anyone noticed bad habits or thought of any suggestions: (First Hangman game)
I've made 2 math programs just to make sure I got it all down pat. The first one (the subject of this question) is a simple addition, multiplication, division, and subtraction program that would deal out random numbers and ask you to add, sub, multiply, or divide them.
#12/14/14
import random
def choose():
num1, num2 = pick_numbers()
types = input("What type would you like Add, Sub, Mutl, Div?\n> ")
if types.upper() == "ADD":
print("Creating your addition problem...")
print("{} + {}".format(num1, num2))
correct_answer = num1 + num2
return correct_answer
elif types.upper() == "SUB":
print("Creating your subtraction problem...")
print("{} - {}".format(num1, num2))
correct_answer = num1 - num2
return correct_answer
elif types.upper() == "MULT":
print("Creating your mutliplecation problem...")
print("{} * {}".format(num1, num2))
correct_answer = num1 * num2
return correct_answer
elif types.upper() == "div":
print("Creating your divison problem...")
print("{} / {}".format(num1, num2))
correct_answer = num1 / num2
return correct_answer
else:
print("Error: Please only choose, add, sub, mult, or div")
def pick_numbers():
num1 = random.randint(1, 100)
num2 = random.randint(1, 100)
return num1, num2
def print_problem():
correct_answer = choose()
while True:
answer = input("What is your answer?\n> ")
if(int(answer) == correct_answer):
print("Correct! The answer was {}".format(answer))
return False
else:
print("Sorry {} isn't correct.".format(answer))
def play_again():
try_again = input("Would you like to play again?\n> ")
if try_again.upper() == "Y":
return True
elif try_again.upper() == "N":
return False
else:
print("Error: please choose only 'Y' or 'N'")
while True:
print_problem()
if not play_again():
break
Do you still notice any bad habits or have any suggestions so I can have better structure or code organization?