Created
December 20, 2015 14:40
-
-
Save JanneSalokoski/72abd8e4a794389291fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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