Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import time
- from tkMessageBox import askyesno
- from tkMessageBox import showinfo
- def is_prime(n):
- '''
- Returns True if the given number is a prime.
- '''
- for x in xrange(2, n):
- if n % x == 0:
- return False
- return True
- def get_primes():
- '''
- Returns a generator for all primes.
- '''
- i = 3
- while True:
- if is_prime(i):
- yield i
- i += 1
- def main():
- '''
- Main routine.
- '''
- now = time.time()
- count = 0
- for prime in get_primes():
- no_prime = random.randint(0, 1)
- result = askyesno('Is it a prime?', prime + no_prime)
- if (no_prime and result) or (not no_prime and not result):
- showinfo(
- 'Fail!',
- 'You are wrong!\nYou answered %s times correct within %.2f seconds.'% (count, time.time() - now)
- )
- break
- count += 1
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement