Back

Multiplication Quiz in command prompt

Sript description

This is a simple quiz. It consists of 10 random multiplication questions. The player can select the answers using the arrow keys and select with space. When the 10 questions are answered, it displays the score.

Result

Example

This uses the previous excersise to display the possible answers.

Source Code

import math
import time
import os
import keyboard
import random
import pygame
def printspaces(spacenum):
    for i in range(0, spacenum):
        print(" ", end="")

def printListWithOutline(Text, Colors, spacing):
    maxLen = 0
    for i in range(0, len(Text)):
        if len(Text[i]) > maxLen:
            maxLen = len(Text[i])
    print("╔", end="")
    for i in range(0, maxLen + spacing):
        print("═", end="")
    print("╗")

    for i in range(0, len(Text)):
        print("║", end="")
        printspaces(math.floor(((maxLen + spacing) / 2) - (len(Text[i])) / 2))
        if Colors[i] == "Red":
            print("\033[91m{}\033[00m".format(Text[i]), end="")
        elif Colors[i] == "Cyan":
            print("\033[96m{}\033[00m".format(Text[i]), end="")
        elif Colors[i] == "White":
            print("\033[97m{}\033[00m".format(Text[i]), end="")
        elif Colors[i] == "Purple":
            print("\033[95m{}\033[00m".format(Text[i]), end="")
        elif Colors[i] == "Black":
            print("\033[30m{}\033[00m".format(Text[i]), end="")
        elif Colors[i] == "Yellow":
            print("\033[93m{}\033[00m".format(Text[i]), end="")
        elif Colors[i] == "LightPurple":
            print("\033[94m{}\033[00m".format(Text[i]), end="")
        elif Colors[i] == "Green":
            print("\033[92m{}\033[00m".format(Text[i]), end="")
        else:
            print(Colors[i], "is not valid")
        printspaces(math.ceil(((maxLen + spacing) / 2) - (len(Text[i])) / 2))
        print("║", )

    print("╚", end="")
    for i in range(0, maxLen + spacing):
        print("═", end="")
    print("╝")

userAnswer = ""

def menu(question, poanswers):
    os.system('cls' if os.name in ('nt', 'dos') else 'clear')
    print(question)
    Colors = ["Green", "White", "White", "White"]
    printListWithOutline(poanswers, Colors, 10)
    selectedItem = 0
    global userAnswer

    run = True
    while run:
        if keyboard.is_pressed("down"):
            if selectedItem < len(poanswers)-1:
                selectedItem+=1
            else:
                selectedItem = 0
            time.sleep(0.1)
            print(selectedItem)

            for i in range(0, len(poanswers)):
                if i == selectedItem:
                    Colors[i] = "Green"
                else:
                    Colors[i] = "White"
            os.system('cls' if os.name in ('nt', 'dos') else 'clear')
            print(question)
            printListWithOutline(poanswers, Colors, 10)
        if keyboard.is_pressed(("up")):
            if selectedItem != 0:
                selectedItem-=1
            else:
                selectedItem = len(poanswers)-1
            time.sleep(0.1)
            print(selectedItem)

            for i in range(0, len(poanswers)):
                if i == selectedItem:
                    Colors[i] = "Green"
                else:
                    Colors[i] = "White"

            os.system('cls' if os.name in ('nt', 'dos') else 'clear')
            print(question)
            printListWithOutline(poanswers, Colors, 10)
        if keyboard.is_pressed("space"):
            userAnswer = poanswers[selectedItem]
            time.sleep(0.1)
            break

score = 0
answers = []
for i in range(10):
    answers = []
    num1 = random.randint(1, 12)
    num2 = random.randint(1, 12)
    Question = "{} x {}".format(num1, num2)
    correctPlace = random.randint(0, 3)
    for o in range(0, 4):
        if o == correctPlace:
            answers.append(str(num1*num2))
        else:
            answers.append(str(random.randint(1, 12) * random.randint(1, 12)))
    menu(Question, answers)
    if str(userAnswer) == str(num1*num2):
        score += 1

os.system('cls' if os.name in ('nt', 'dos') else 'clear')
print("")
print("Congratulations!! your score was: ", score)