Advertisement
Ayuto

Primes game

May 22nd, 2014
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. import random
  2. import time
  3.  
  4. from tkMessageBox import askyesno
  5. from tkMessageBox import showinfo
  6.  
  7.  
  8. def is_prime(n):
  9.     '''
  10.     Returns True if the given number is a prime.
  11.     '''
  12.  
  13.     for x in xrange(2, n):
  14.         if n % x == 0:
  15.             return False
  16.  
  17.     return True
  18.  
  19. def get_primes():
  20.     '''
  21.     Returns a generator for all primes.
  22.     '''
  23.  
  24.     i = 3
  25.     while True:
  26.         if is_prime(i):
  27.             yield i
  28.  
  29.         i += 1
  30.  
  31.  
  32. def main():
  33.     '''
  34.     Main routine.
  35.     '''
  36.    
  37.     now = time.time()
  38.     count = 0
  39.     for prime in get_primes():
  40.         no_prime = random.randint(0, 1)
  41.         result = askyesno('Is it a prime?', prime + no_prime)
  42.         if (no_prime and result) or (not no_prime and not result):
  43.             showinfo(
  44.                 'Fail!',
  45.                 'You are wrong!\nYou answered %s times correct within %.2f seconds.'% (count, time.time() - now)
  46.             )
  47.             break
  48.            
  49.         count += 1
  50.            
  51.  
  52. if __name__ == '__main__':
  53.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement