Need an exit button for my project

Need help for a project I’ve been doing, everything that I’ve coded so far works but I can’t find a solution to what I’ve been looking for. I need an ‘x’ button in the quiz that ends it, shows me my score and sends me back to menu. I’ve been trying but I can’t find anything, would appreciate the help.

Here’s the code:


import random
import operator
def menu():
    print("Welcome to the Maths Quiz game. Please choose from the menu below:")
    print("-" * 60)
    print("[A] New Game")
    print("[B] View Today's High Scores")
    print("[C] View Past High Scores")
    print("[D] Exit")

menu()
option = str(input("What would you like to do?"))

while option != "D":

    if option == "A":
        #print("New Game")
        name = input("What is your name?")
        print("Hello " + name.title() + "!")

        def quiz():
            print('Welcome! This is a 10 question quiz!\n')
            score = 0
            for i in range(10):
                correct = askQuestion()
                if correct:
                    score += 1
                    print('Correct!')
                else:
                    print('Incorrect!')
            print('Your score was {}/10'.format(score))


        def askQuestion():
            answer = randomCalc()
            guess = float(input())
            return guess == answer


        def randomCalc():
            ops = {'+': operator.add,
                   '-': operator.sub,
                   '*': operator.mul,
                   '/': operator.itruediv}
            (num1) = random.randint(1, 10)
            num2 = random.randint(1, 10)
            op = random.choice(list(ops.keys()))
            answer = ops.get(op)(num1, num2)
            print('What is {} {} {}?'.format(num1, op, num2))
            return answer



        quiz()

I’ve got the rest figured out but I can’t finish what I wanted with the quiz, it’d be awesome if I could get some help with that, thanks.

Hi.

Does this do what you have in mind?

import random
import operator


def menu():
    MENU = {
        'A': "New Game",
        'B': "View Today's High Scores",
        'C': "View Past High Scores",
        'D': "Exit"
    }
    for option in MENU:
        print(f"[{option}] {MENU[option]}")
    print()
    return MENU.keys()


def start():
    name = input("What is your name? ")
    print("Hello " + name.title() + "!")
    quiz()


def quiz():
    print('Welcome! This is a 10 question quiz!\n')
    score = 0
    for i in range(10):
        correct = askQuestion()
        if correct == "X":
            break
        elif correct:
            score += 1
            print('Correct!')
        else:
            print('Incorrect!')
    print('Your score was {}/10'.format(score))


def askQuestion():
    answer = randomCalc()
    guess = input().upper()
    if guess == "X":
        return guess
    else:
        try:
            guess = float(guess)
        except ValueError:
            return
    return guess == answer


def randomCalc():
    ops = {'+': operator.add,
           '-': operator.sub,
           '*': operator.mul,
           '/': operator.itruediv}
    (num1) = random.randint(1, 10)
    num2 = random.randint(1, 10)
    op = random.choice(list(ops.keys()))
    answer = ops.get(op)(num1, num2)
    print('What is {} {} {}?'.format(num1, op, num2))
    return answer


print("Welcome to the Maths Quiz game. Please choose from the menu below:")
while True:
    print()
    options = menu()
    option = input("What would you like to do? ").upper()
    print()

    if option not in options:
        print("Please try again.")
        print()
    elif option == "A":
        start()
    elif option == "B":
        pass  # to do
    elif option == "C":
        pass  # to do
    elif option == "D":
        print("Thank you and have a good day!")
        break

I could not resist correcting one or two mistakes for you and improving on the logic flow, but that aside, it’s still all your own work.

If you have any questions, feel free to ask.

Btw: I don’t think that I’ve introduced any bugs, but if I have and you can’t squish them, lmk.

1 Like

Yes, thank you so much, this does exactly what I thought of! I’m still a beginner so thanks a lot for helping me and adjusting my code ! :slight_smile:

1 Like

You’re welcome.

For a beginner, you’ve done a very good job. Keep at it and happy coding.

1 Like