Python Math Game

This is a program I made for my daughter to sharpen her multiplication skills. This program is a multiplication quiz game that asks the player 10 random multiplication questions. The game tracks the number of correct and incorrect answers, records how long the player takes to answer each question, and saves the results in a text file called multiplication_results.txt.

She enjoys playing with this program even if it is just a plain text game. Below is the source code of version 1. If you want a copy of the program leave a comment below. Stay tuned for the next version, which is coming soon!

import random
import datetime
import os

def main():
    os.system('cls' if os.name == 'nt' else 'clear')

    startTime = datetime.datetime.now()
    y = 0
    n = 0

    print("\n\033[1;32m ********** MULTIPLICATION **********\n")
    print("\033[1;32m This program is created by Abe")
    print("\n\033[1;32m ************************************\n")

    # Open a file to write the output
    with open("multiplication_results.txt", "a") as f:
        f.write(f"Game started at: {startTime}\n")
        f.write("********** MULTIPLICATION **********\n")

        for counter in range(10):
            a = random.randint(0, 10)
            b = random.randint(0, 10)
            
            print("\033[1;37m **********************************")
            print(f" {a} x {b} ")
            correctAnswer = a * b
            print("\033[1;37m **********************************\033[1;30m")
            
            # Start tracking the time
            questionStart = datetime.datetime.now()

            answer = None
            while answer is None:
                try:
                    answer = int(input("Enter your answer: "))
                except ValueError:
                    print("\033[1;31m Invalid Entry \033[1;30m")

            # End tracking the time and calculate the duration
            questionEnd = datetime.datetime.now()
            timeTaken = questionEnd - questionStart  # Calculate how long they took
            
            if correctAnswer == answer:
                print(f"\n\033[1;32m You are correct. The answer is {correctAnswer}")
                y += 1
                f.write(f"Question {counter+1}: {a} x {b} = {correctAnswer} (Correct), Time Taken: {timeTaken}\n")
            else:
                print(f"\n\033[1;31m You are wrong. The answer is {correctAnswer}")
                n += 1
                f.write(f"Question {counter+1}: {a} x {b} = {correctAnswer} (Wrong), Time Taken: {timeTaken}\n")
            
            print(f"\n \033[1;34m Score: {y}/{counter+1}\n")
            print(f" \033[1;34m Time Taken: {timeTaken} seconds\n")
        
        endTime = datetime.datetime.now()
        final_score = f"\n \033[1;34m Final Score: {y}/10\n"
        print("\033[1;37m ***********************")
        print(final_score)
        print("\033[1;37m ***********************")
        
        # Write final score and end time to file
        f.write(f"\nFinal Score: {y}/10\n")
        f.write(f"Game ended at: {endTime}\n")
        f.write("***********************\n\n")
        
    # Wait for the user to press any key to exit
    input("\nPress any key to exit...")

if __name__ == "__main__":
    main()

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.