0
import random
from tkinter import *
import tkinter as tk
import time

scores = []
score = 0

#introduction
def start():
    print("               Welcome to Maths Smash                    \n")
    print("Complete the questions the quickest to get a better score\n")

    username = True #this makes sure the user can only enter characters a-z
    while username:
        name = input("What is your name? ")
        if not name.isalpha():
            print("Please enter letters only")
            username = True
        else:
            print("Ok,", name, "let's begin!")
            main()

#menu
def main():
    choice = None
    while choice !="0":
        print(
        """
        Maths Smash

        0 - Exit
        1 - Easy
        2 - Medium
        3 - Hard
        4 - Extreme
        5 - Dashboard
        """
        )

        choice = input("Choice: ")
        print()

    #exit
        if choice == "0":
            print("Good-bye!")
            quit()
    #easy
        elif choice == "1":
            print("Easy Level")
            easy_level()
    #medium
        elif choice == "2":
            print("Medium Level")
            medium_level()
    #hard
        elif choice == "3":
            print("Hard Level")
            hard_level()
    #extreme
        elif choice == "4":
            print("Extreme Level")
            extreme_level()
    #teacher login
        elif choice == "5":
            print("Dashboard")
            from GUI import dashboard
            dashboard()     
        else:
            print("Sorry but", choice, "isn't a vaild choice.")

def easy_level():
    global score
    #easy level
    for i in range(10):
        operator_sign =""
        answer = 0
        num1 = random.randint(1,5)
        num2 = random.randint(1,5)
        operator = random.randint(1,4)
        #choosing the operator randomly
        if operator == 1:
            operator_sign = " + "
            answer = num1 + num2
        elif operator == 2:
            operator_sign = " - "
            answer = num1 - num2
        elif operator == 3:
            operator_sign = " * "
            answer = num1 * num2
        elif operator == 4:
            operator_sign = " / "
            num1 = random.randint(1,5) * num2
            num2 = random.randint(1,5) 
            answer = num1 // num2
        else:
            print("That is not a vaild entry!\n")
        #outputting the questions along with working out if it's correct        
        question = str(num1) + operator_sign + str(num2) + "\n"
        user_input = int(input(question))
        if user_input == answer:
            score = score + 1
    #total score
    print("You scored:", str(score), "out of 10")

Been trying to add a timer in this game for a while but every time I go to the first level it doesn't work with the game at the same time, as in I will load the first level and none of the questions will pop-up until the timer has completed. Was wondering if anyone could help me or know a fix to this, I want to make it so if the user completes the test within a certain amount of time they get an additional 10 points added to the overall score. This is what I had come up with at first:

def timer():
    countdown=120
    while countdown >0:
        time.sleep(1)
        score = score + 10
Brandon Brock
  • 52
  • 2
  • 6
  • You can use this answer: https://stackoverflow.com/a/8600301/5751251 – DSCH Nov 30 '17 at 17:59
  • Trying to run a timer while also allowing the user to interact with the program gets into concepts you may not be ready for yet. Simpler to just check the time when you start (`time.time()`), then check again after the user gives a correct answer. Subtract the two to get the number of seconds elapsed, and calculate score based on that. – glibdud Nov 30 '17 at 18:34

0 Answers0