import random def reset_variables(): 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) # Addition game def play_one_Addition_game(): global right global wrong # Get random numbers to quiz user rand_one = random.randrange(1,10) rand_two = random.randrange(1,10) # Get the answer 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 "5. exit" # fill in rest # 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?") if reply == 1: for game_num in range(0,10): play_one_Addition_game() elif reply == 2: play_Subtraction_game() elif reply == 3: play_Multiplacation_gmae() elif reply == 4: play_Division_game() elif reply == 5: print "thx for playin" break else: # I don't know what to do! print "Choose 1, 2, 3, 4 or 5 to exit" print "You got " + str(right) + " correct and " + str(wrong) + " wrong." # Game is over now # Reset variables 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