Skip to content

Instantly share code, notes, and snippets.

@JanneSalokoski
Created December 20, 2015 14:40
Show Gist options
  • Save JanneSalokoski/72abd8e4a794389291fe to your computer and use it in GitHub Desktop.
Save JanneSalokoski/72abd8e4a794389291fe to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import random
import time
import sys
def getNewQuestion(x, y):
print("{:^4} + {:^4} = ".format(x, y), end="")
try:
result = input()
if result.lower() == "q" or result.lower() == "quit":
print("\033[{}C\x1b[1A\033[93m \t[Exit]\033[0m".format(15 + len(str(result))))
sys.exit()
result = int(result)
if result != x + y:
print("\033[{}C\x1b[1A\033[91m \t[Incorrect]\033[0m".format(15 + len(str(result))))
getNewQuestion(x, y)
else:
print("\033[{}C\x1b[1A\033[92m \t[Correct]\033[0m".format(15 + len(str(result))))
except ValueError:
print("\033[{}C\x1b[1A\033[91m \t[Incorrect]\033[0m".format(15 + len(str(result))))
getNewQuestion(x, y)
except KeyboardInterrupt:
print("\033[93m \t[Exit]\033[0m".format(17))
sys.exit()
def main():
random.seed(time.clock())
while True:
x = random.randrange(0,99)
y = random.randrange(0,99)
getNewQuestion(x, y)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment