Python Forum
How to print multiple elements from multiple lists in a FOR loop?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print multiple elements from multiple lists in a FOR loop?
#6
You would get a lot of benefit from writing some classes to manage your test and questions. Something like this (but much better):
import random

class Question:
    def __init__(self, question=''):
        self.question = question
        self.answers = []
        self.correct_answer = 0

    def ask(self):
        while True:
            print(f'\nQuestion: {self.question}')
            for i, answer in enumerate(self.answers):
                print(f'{i+1}: {answer}')
            try:
                answer = int(input('Enter answer: '))
                if not answer in range(1, len(self.answers)+1):
                    raise ValueError
                return answer
            except ValueError:
                print('Please enter the number of your answer')

    def print(self):
        print(f'\nQuestion: {self.question}')
        for i, answer in enumerate(self.answers):
            print(f'{i+1}: {answer}')
        print(f'Correct answer : {self.correct_answer}')

def make_quiz():
    quiz = []
    while True:
        text = input('Enter question: ')
        if len(text) == 0:
            return quiz
        q = Question(text)

        while len(q.answers) < 4:
            text = input(f'Enter answer {len(q.answers)+1}: ')
            if len(text) > 0:
                q.answers.append(text)
            elif len(q.answers) < 2:
                print('There must be at least two answers')
            else:
                break; # done entering answers

        q.correct_answer = q.ask()

        q.print()
        if input('Add this question to the quiz? (y/n): ')[0] in ('Yy'):
            quiz.append(q)

def take_quiz(quiz):
    questions = random.sample(quiz, k=len(quiz))
    score = 0
    for q in questions:
        answer = q.ask()
        if answer == q.correct_answer:
            score += 1
        else:
            print(f'The correct answer is {q.correct_answer}: {q.answers[q.correct_answer-1]}')
        print('\n\n')
    print(f'Your score is {score} out of {len(questions)}')

print('Enter questions for the quiz')
quiz = make_quiz()
print('\n\nTake the quiz')
take_quiz(quiz)
You would want a way to store a quiz in a file and load a quiz from a file. You might want a way to edit a quiz, though it may be easier to just edit the quiz file. Unless the file is encrypted.
bowlofred likes this post
Reply


Messages In This Thread
RE: How to print multiple elements from multiple lists in a FOR loop? - by deanhystad - Dec-02-2020, 06:16 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Project Structure for Modularity and Reusability with Multiple Entry Points b19wh33l5 0 150 Apr-24-2024, 12:21 PM
Last Post: b19wh33l5
  Using a script to open multiple shells? SuchUmami 9 548 Apr-01-2024, 10:04 AM
Last Post: Gribouillis
  __init__() got multiple values for argument 'schema' dawid294 4 2,409 Jan-03-2024, 09:42 AM
Last Post: buran
  problem with print lists MarekGwozdz 4 697 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
  python convert multiple files to multiple lists MCL169 6 1,568 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Multiple variable inputs when only one is called for ChrisDall 2 496 Oct-20-2023, 07:43 PM
Last Post: deanhystad
  Can I use logging in a class (without multiple messages) mevan 2 610 Oct-16-2023, 11:08 PM
Last Post: mevan
  Search for multiple unknown 3 (2) Byte combinations in a file. lastyle 7 1,382 Aug-14-2023, 02:28 AM
Last Post: deanhystad
Question Using SQLAlchemy, prevent SQLite3 table update by multiple program instances Calab 3 764 Aug-09-2023, 05:51 PM
Last Post: Calab
  What's the best way for multiple modules to handle database activity? SuchUmami 3 663 Jul-08-2023, 05:52 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020