hvpnotic
hvpnotic

Reputation: 33

How can i simplify my python quiz? (make it shorter)

I have a python quiz made and it is around 500 lines. i was wondering how i could make it shorter, and simplify the code. This is an example of one of my questions in the quiz

while counter<3:
    def question(question,choices):                  
        print(question)                        
        for question in choices:             
            print(question) 

    print('\033[0m'"____________________________________________________________\n")

    question("Question 1. What is the real name of Batman?", ["A. Bruce Wayne", "B. Peter Parker", "C. Bruce Banner", "D. Bruce Waine"])
    answer = input().lower()

    if answer == "a":
        print('\033[32m'"\nNice job! ✔\n")
        score = score +1
        counter = 4
    elif answer == "bruce wayne":
        print('\033[32m'"\nGreat work! ✔\n")
        counter = 4
        break
    else:
        score = score - 1
        counter = counter +1
        if counter ==3:
            print('\33[31m'"\nIncorrect! ✘ The correct answer is A. Bruce Wayne\n")
        elif counter ==1 or 2:
            print('\33[31m'"\nIncorrect! ✘ Try again...\n")
    print('\033[0m''\033[04m'"Your score is ",score)

Upvotes: 3

Views: 137

Answers (1)

Collin Heist
Collin Heist

Reputation: 2682

Below is an example of a custom class that defines a "question" - you could then make many of them, and reuse a lot of code, this way.

class Question:
    def __init__(self, number, question, choices, correct, chances=3):
        self.number = number
        self.question = question
        self.choices = choices
        self.correct = correct
        self.chances = chances

    def print(self):
        print(self.question, '\n', '\n'.join(self.choices))

    def guess(self):
        while self.chances:
            answer = input().lower()

            if answer in self.correct:
                print('\033[32m'"\nNice job! ✔\n")

                return True
            else:
                self.chances -= 1
                if self.chances == 0:
                    print('\33[31m\nIncorrect! ✘ The correct answer is', self.correct)
                    return False
                else:
                    print('\33[31m'"\nIncorrect! ✘ Try again...\n")

# Example setup
score = 0

all_questions = [
    Question(
        0,
        'What is the real name of Batman?',
        ['A. Bruce Wayne', 'B. Peter Parker', 'C. Bruce Banner', 'D. Bruce Waine'],
        ['a', 'bruce wayne']
    ),
    Question(
        1,
        'Another question..',
        ['A. Answer 1', 'B. Answer 2', 'C. Answer 3', 'etc..'],
        ['b', '3'],
    )
]

for question in all_questions:
    question.print()
    correct = question.guess()
    if correct:
        score += 1

I have shown an example of how you would make many questions (in a list) and then print and guess all of them, one-by-one.

Let me know what questions (ha) you have.

Upvotes: 2

Related Questions