Python Forum
Pointer in the right direction?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pointer in the right direction?
#1
Hey

I have created a small quiz to learn some Python. I would like to figure out how to make the code more effective.

My solution now would be to manually copy/paste the code one more time and change questionList[0] into questionList[1] (for each question in the quiz) and so forth.

I am sure there is a smarter way to do this.

Any tips or ideas?

In any case I would appreciate a pointer in the right direction!

.......................................................
print(questionList[0].question)

for i in range(3):
    answer = input("Response: ")
    if answer == (questionList[0].answer):
        print("Correct Answer")
        break
    if answer != questionList[0].answer:
        continue
else:
    print("Wrong Answer")
    print("Correct answer was:", questionList[0].answer)
Reply
#2
Your formatting got lost because you didn't use the proper tags. But I think you want this:
print(questionList[0].question)

for i in range(3):
    answer = input("Response: ")
    if answer == (questionList[0].answer):
        print("Correct Answer")
        break  # Break out of the loop
    else:
        print("Wrong Answer")
else:
    # Only executed after 3 wrong guesses
    print("Correct answer was:", questionList[0].answer)
As for making the code more effective, a quiz should have more than one question. I would make a quiz program that loads quiz questions so you don't have to take the same quiz over and over or write a new program to take a different quiz.

I would shuffle the questions so they don't always come in the same order.

The quiz program should e able to handle different numbers of questions and compute a score.

I would support multiple types of questions, Some multiple choice with only one guess or multiple guesses with a decreasing score for each guess. Some fill in the blank with maybe multiple guesses. Some true or false questions.

Your program should allow for the player to type the answer in upper, lower or mixed case.
Reply
#3
Thanks! I am going to figure out the tagging part better. I have played around making multiple choices and some other ideas as well. I will add more questions later, now it is more about learning.

What I am trying to solve is that for each question in the quiz I have to copy/paste the code manually again. My thinking was that there must be a way to automate that.

First time: questionList[0]

Second time: questionList[1]

and so forth...

But perhaps it is a non-issue! :)

This is what it looks like at the moment:

class Question:
  def __init__(self, question, answer):
    self.question = question
    self.answer = answer

questionList = []
questionList.append(Question("What is the capital of Colombia", "Bogota"))
questionList.append(Question("What is the capital of Chile?", "Santiago"))

print(questionList[0].question)

for i in range(3):
    answer = input("Response: ")
    if answer == (questionList[0].answer):
        print("Correct answer")
        break
    if answer != questionList[0].answer:
        continue
else:
    print("Wrong Answer")
    print()
    print("The correct answer was:", questionList[0].answer)

print(questionList[1].question)

for i in range(3):
    answer = input("Response: ")
    if answer == (questionList[1].answer):
        print("Correct answer")
        break
    if answer != questionList[1].answer:
        continue
else:
    print("Wrong Answer")
    print()
    print("The correct answer was:", questionList[1].answer)
Reply
#4
A class is a good idea. Can also used lists, tuples or named tuples. This uses a list of tuples.
questions = [ \
    ("What is the capital of Colombia?", "Bogota"),
    ("What is the capital of Chile?", "Santiago")]

for question, answer in questions:
    # Ask the question
    ...
A nice thing about classes is you could do subclassing for different kinds of questions. They would share some basic methods like asking the question and verify the answer. You might have a fill in the blanks class, a T/F, a multiple choice, beat the buzzer type questions, etc. Each of these questions would know how to ask the question in the context of your quiz program and how to evaluate the answer. Eventually you could support questions that use multimedia.

As for typing questions into the program, that is something I would only do to at a very short while. Eventually you will want a way for your quiz program to import questions from some other resource. Maybe that will be a json file. Maybe a sql database. Maybe you will scrape questions from the internet. Maybe there is a network of quiz makers and they have a standard format and set of tools.
Reply
#5
Dictionary would be a good fit for this,so question is key and answer is value.
So it easier to write a example of this than try to fit it this into your code that have some problems.
class Quiz:
    def __init__(self, questions, score):
        self.questions = questions
        self.score = score

    def quiz(self):
        keys = self.questions.keys()
        for quiz_numb, question in enumerate(keys, 1):
            answer = input(f'{quiz_numb}. {question}')
            if self.questions[question] == answer:
                print('Correct answer\n')
                self.score += 1
            else:
                print(f'Not correct,the answer was <{self.questions[question]}>\n')
        print(f'Total score for this round {self.score} correct answer')

if __name__ == '__main__':
    # Can come from a external soruce like a simple DB or json
    questions = {
     "What is the capital of Colombia? ": "Bogota",
     "What is the capital of Chile? ": "Santiago"
    }

    # Run quiz
    round_1 = Quiz(questions, score=0)
    round_1.quiz()
Output:
1. What is the capital of Colombia? Bogota Correct answer 2. What is the capital of Chile? Lima Not correct,the answer was <Santiago> Total score for this round 1 correct answer
Reply
#6
Thanks to both of you! I really appreciate the input, it was most helpful!

I am just starting out so these kind of pointers are invaluable at this stage.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Project Direction bclanton50 1 1,334 Jan-06-2022, 11:38 PM
Last Post: lucasbazan
Question How to understand the vector/direction mason321 0 1,119 Dec-14-2021, 10:57 PM
Last Post: mason321
  General pointer to start data4speed 3 1,970 Jul-01-2020, 06:18 AM
Last Post: DPaul
  Length and direction cosines of lines tarikrr 1 1,773 Nov-15-2019, 04:16 AM
Last Post: SheeppOSU
  Some direction needed Patriot1017 3 2,505 Sep-03-2019, 05:44 PM
Last Post: jefsummers
  Practicing using a "flag": please point in right direction magsloo 5 3,109 May-10-2019, 04:58 AM
Last Post: perfringo
  VM address (C pointer) of Python object Skaperen 1 2,054 Apr-21-2019, 10:57 PM
Last Post: hshivaraj
  trace invalid pointer simon149 7 5,116 Apr-16-2019, 07:05 AM
Last Post: simon149
  How to create a graph for direction visualization Visiting 2 2,800 Sep-22-2018, 10:49 PM
Last Post: Visiting
  Need tutorial/direction to access shared memory ridshack 2 2,992 Feb-22-2018, 11:24 PM
Last Post: ridshack

Forum Jump:

User Panel Messages

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