#Addition import random right = 0 wrong = 0 def reset_variables(): global right global wrong right = 0 wrong = 0 reset_variables() def ask_for_integer(ask): while True: answer = raw_input(ask) if not answer.isdigit(): print "Please enter a number." else: return int(answer) def reply(): reply = ask_for_integer("What is " + str(rand_one) + " plus " + str(rand_two) + "?") def play_one_Addition_game(): global right global wrong rand_one = random.randrange(1,10) rand_two = random.randrange(1,10) answer = rand_one + rand_two reply = ask_for_integer("What is " + str(rand_one) + " plus " + str(rand_two) + "?") if reply == answer: print "YOu rock" right +=1 else: print "You got it wrong, YOu suck" print "The answer is " + str(answer) wrong +=1 # Print out menu for user def print_menu(): print "1. Addition" print "2. Subtraction" print "3. Multiplacation" print "4. Division" def ask_for_yn(ask): while True: answer = raw_input(ask) if answer[0] <> "y" and answer[0] <> "n": print "please enter a y or n." else: return answer # Find out what game user wants to play, and play it def ask_for_operation(): while True: print_menu() reply = ask_for_integer("What would you like to play?") for game_num in range(0,10): if reply == 1: play_one_Addition_game() elif reply == 2: play_Subtraction_game() elif reply == 3: play_Multiplication_game() else: reply == 4 play_Division_game() answer = ask_for_yn("Would you like to play again?") if answer == "n": print "You got " + str(right) + " correct and " + str(wrong) + " wrong." break #Subtraction Subtraction = 2 def play_Subtraction_game(): global right global wrong rand_one = random.randrange(1,10) rand_two = random.randrange(1,10) answer = rand_one - rand_two print "What is", str(rand_one), "minus", str(rand_two), "?" reply = ask_for_integer("answer") if reply == answer: print "YOu rock" right +=1 else: print "You got it wrong, YOu suck" + str(answer) wrong +=1 print "You got", int(right)," correct and", int(wrong)," wrong." #Multiplaction Multiplication = 3 def play_Multiplication_game(): global right global wrong rand_one = random.randrange(1,10) rand_two = random.randrange(1,10) answer = rand_one * rand_two print "What is", str(rand_one), "times", str(rand_two), "?" reply = ask_for_integer("answer") if reply == answer: print "YOu rock" right +=1 else: print "You got it wrong, YOu suck" + str(answer) wrong +=1 #Division Division = 4 def play_Division_game(): global right global wrong rand_one = random.randrange(1,10) rand_two = random.randrange(1,10) answer = rand_one / rand_two print "What is", str(rand_one), "divided by", str(rand_two), "?" reply = ask_for_integer("answer") if reply == answer: print "YOu rock" right +=1 else: print "You got it wrong, YOu suck" + str(answer) wrong +=1 while True: if reply == 1: play_one_Addition_game() elif reply == 2: play_Subtraction_game() elif reply == 3: play_Multiplication_game() else: reply == 4 play_Division_game() play_one_Addition_game() play_Subtraction_game() play_Multiplication_game() play_Division_game() print "You got " + str(right) + " correct and " + str(wrong) + " wrong." reset_variables() ask_for_operation()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question