Created
August 31, 2018 08:33
-
-
Save Chaitran77/2f7bdf6429db4274be5b628b5923fcbe to your computer and use it in GitHub Desktop.
Kiran's Interactive Times Tables created by chaitran77 - https://repl.it/@chaitran77/Kirans-Interactive-Times-Tables
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
import random | |
import time | |
PROMPT = "--->" | |
ALLOWED_TABLES = (1, 12) | |
question = () # first element = first multiple, second element | |
answer = None | |
no_of_questions = 10 # or infinate unitl one wrong | |
no_correct_answers = 0 | |
start_time = time.time() | |
def set_question(): | |
global question | |
question = (random.randint(ALLOWED_TABLES[0], ALLOWED_TABLES[1]), | |
random.randint(ALLOWED_TABLES[0], ALLOWED_TABLES[1])) | |
def ask_question(): | |
global answer | |
answer = input("What is {}x{}? \n {}".format(question[0], question[1], | |
PROMPT)) | |
def check_answer(): | |
global no_correct_answers | |
correct_answer = question[0] * question[1] | |
try: | |
if int(answer) == correct_answer: | |
print(" Correct!\n") | |
no_correct_answers += 1 | |
else: | |
print(" Incorrect!") | |
print(" The correct answer is {}\n".format(correct_answer)) | |
except ValueError: | |
print(" Impossible to compute! Try again:\n") | |
ask_question() # the question will still be the same | |
check_answer() | |
def process_time(time_in_seconds): | |
time_parts = str(time_in_seconds).split(".") | |
minutes = int(time_parts[0]) // 60 | |
seconds = int(time_parts[0]) % 60 | |
frac_second = int(time_parts[1]) | |
frac_second = str(round(time_in_seconds, | |
2)).split(".")[1] # for a bit of accuracy | |
return "{}:{}.{}".format(minutes, seconds, frac_second) | |
def start(): | |
for i in range(3, 0, -1): | |
print(i) | |
time.sleep(1) | |
print("Begin!\n") | |
for i in range(no_of_questions): | |
set_question() | |
ask_question() | |
check_answer() | |
def end(): | |
end_time = time.time() - start_time | |
print("You scored {} out of {}!".format(no_correct_answers, | |
no_of_questions)) | |
#print("That's {}% of your answers correct!".format()) | |
print("It took you {} minutes to complete.".format(process_time(end_time))) | |
print("Well Done!") | |
def play_again(): | |
again = input("Would you like to play again?") | |
if "y" in again.lower(): | |
print("Okay, here we go!\n") | |
start() | |
end() | |
elif "n" in again.lower(): | |
print("Thanks for using the Interactive Times Tables program. \nThis program was created by Kiran Patel on the 13th of May 2018.") | |
else: | |
print("Not understood, try again:") | |
again() | |
play_again() | |
start() | |
end() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment