Advertisement
Guest User

Math Game Basic Functions

a guest
Sep 9th, 2013
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. import time, random, sys
  2. from tkinter import *
  3. from threading import Timer
  4.  
  5. #Global variables to be set
  6.  
  7. global endGame
  8. global gameTime
  9. endGame = False
  10. gameTime = 20
  11. #functions required throughout the game
  12. def runTimer(x):    
  13.     t = Timer(x,time_up) # x is amount of time in seconds
  14.     t.start()
  15.     mathFunction()
  16. def time_up():
  17.     print('your time is up!')
  18.     global endGame
  19.     endGame == True
  20.     print (endGame)
  21.     print('your time is up!')
  22.    
  23. def mathFunction():
  24.     global endGame
  25.     while endGame == False:
  26.         Int1 = random.randint(0,20)
  27.         Int2 = random.randint(0,20)
  28.         Int3 = random.randint(0,20)
  29.  
  30.         if Int1 > Int2:
  31.             answer = (Int1 - Int2) * Int3
  32.             print ('(',Int1,'-',Int2,') x ',Int3)
  33.            
  34.         elif Int2 > Int1:
  35.             answer = (Int2 - Int1) * Int3
  36.             print ('(',Int2,'-',Int1,') x ',Int3)
  37.            
  38.         userInput = 0
  39.        
  40.         while True:
  41.            
  42.           try:
  43.              userInput = int(input("Enter your answer: "))      
  44.           except ValueError:
  45.              print("Not an integer!")
  46.              continue
  47.           else:
  48.              
  49.               if userInput == answer:
  50.                   print('Correct')
  51.                   break
  52.               else:
  53.                   print('Wrong')
  54.                   break
  55.    
  56.    
  57.        
  58.  
  59.  
  60. #Display the first input as to whether the player would like to play
  61.  
  62. choice = ""
  63.  
  64. while choice != "n":
  65.  
  66.     choice = str(input('Would you like to play?(y/n)'))
  67.    
  68.     if choice == 'y':
  69.         runTimer(20)  
  70.         while endGame == False:
  71.             mathFunction()
  72.     elif choice == 'n':
  73.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement