Python Forum
help setting questions and answers in quiz game?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help setting questions and answers in quiz game?
#1
Hello! So I'm working on a quiz game as a project for my CS class, and what I'm trying to do is have my program read each line in a text file, and determine which lines are the questions, and which are the answers. The structure of the text file is: an all lower case question line, followed immediately in a new line with an all caps answer. I structured it that way so that I could easier set answers and questions based off of what is uppercase, and what is lowercase.

So far, this is what I have in my Movie and Game classes:

class Movie:
    #sets up question and answer pair - in which a movie name is the proper answer
    def __init__(self, question, movie):
        self._question = question
        self._movie_answer = movie
        
    def get_question(self):
        #get the question
        return self._question
    
    def get_movie_answer(self):
        #get the answer to the question (movie name)
        return self._movie_answer

class Game:
    def __init__(self):
        self._qs = []
        self._as = []
        with open(movies_text, 'r') as f:
            for line in f:
                index = 0
                if line.isLower():
                    line = self._question.append(self._qs)
                    line[index+1] = self._movie_answer.append(self._as)
                    index +=1
                else:
                    line = self._movie_answer.append(self._as)
                    line[index-1] = self._question.append(self._qs)
                    index +=1
Basically what i'm trying to do in the code above is have the program read each line, and if it starts and is all lowercase, then the program will know to set that to the question, and it also knows then that the following line will be the answer. Otherwise, the program is starting on a line that is all caps, and it knows that the previous line is the question.

Im rather confused from here as to how I am supposed to actually get the question to read in a program, so that the user can actually try to test this quiz, and have the assigned answer follow? Would I just print off each corresponding index items in each question and answer lists (such as self._qs[1], and self._as[1])? This is all new to me, so I apologize if I butcher any terms, or don't use proper form! Also, the text file i'm reading from is called 'movies_text.txt'.

As a reference example, the structure in the text file of questions and answers is as follows:
what movie did tom hanks star in with hale berry?
CLOUD ATLAS

--
For my class project, we have to make some kind of game using GUI's, with Python - and Tkinter(if necessary). A lot of people make things like the snake game, or space invaders, etc. For my quiz game, i'm first trying to just get the quiz portion working, before I actually go in and set up the GUI. Hopefully that is enough information for context! Let me know if there's anything I can clarify!
Reply
#2
Sorry to say it, but your code/design has so many problems it's hard to decide where to start with.
So let's address your main concern/answer - lines 22-29 are total mess:
1. you mix properties from both classes - self._question and self._movie_answer are not defined in your Game class, i.e. it does not have properties ._question and ._movie_answer
2. you try to append empty lists - self._qs and self._as respectively, to them (assuming they are list). You need to append line to these lists, not append them to non-existent properties . Don't forget to strip '\n' from the end of line though.
3. Having separate self._qs and self._as properties makes the class Movie useless - i.e. you keep the questions and answers in two lists, properties of class Game.
4. because you can use index/enumerate you don't need to use lower/upper case for questions/answers in the text file
5. movies_text is not defined

class Game:
    def __init__(self, quiz_file):
        self.quiz_file = quiz_file
        self.questions = []
        self.answers = []
        with open(self.quiz_file, 'r') as f:
            for i, line in enumerate(f):
                if i%2: # this is answer
                    self.answers.append(line.strip())
                else: # this is question
                    self.questions.append(line.strip())
                    
quiz = Game('movies_text.txt') # change this to your file name, incl. full path
print(quiz.questions)
print(quiz.answers)
So, first have a look at the code above and make sure you understand what I have done. Then we can take the next step and improve the design
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thank you so much @buran for all of your help - I can see what you mean about certain areas in my code being unecessary or useless ins ome areas, or not defined later on; and I greatly appreciate your cleaner version of code; it is easy to understand for a beginner like me, and looks much nicer.

I do have a question though, in your given code, on line 8, what does the i%2 do? I am trying to figure that out to myself but I can't come to a conclusion. Is it asking if each line is an even number?

Also, on a separate note, with two lists of questions and answers, I went ahead and made a function to print out each specific question, along with its given answer. My function definition(and following code) looks like this:

    def get_question(self):
        index = 0
        for item in self.questions:
            answer = self.answers[index]
            print(item)
            print(answer)
            index +=1
                               
                     
quiz = Game('movies_text.txt') # change this to your file name, incl. full path
#print(quiz.questions)
#print(quiz.answers)
print(quiz.get_question())
After i tested this, it showed me each question line, with each following answer - which is great because that is what I wanted. However, how do I run this function, and only get one question + answer pair at a time? Or, how do I set this up so that the user goes through each question when they want to move on, instead of displaying every question and answer pair? Thanks so much!
Reply
#4
(May-10-2018, 02:55 AM)yoyoitsjess Wrote: I do have a question though, in your given code, on line 8, what does the i%2 do? I am trying to figure that out to myself but I can't come to a conclusion. Is it asking if each line is an even number?

% is modulo operator. The result is the remainder of the division. So, yes, in this case i%2 checks for odd/even number. Remember indexing (thus also enumerate) starts from 0. So if there is no reminder - it's a question (index is 0, 2, 4, etc.) and if there is reminder - it's answer (index is 1, 3, 5, etc.). Also remember that non-zero numbers are evaluated as True.
using [the same] index is the most primitive/naive way to link question and answer. Not very pythonic though. Also note my use of enumerate, no need to use/increase index yourself.
more pythonic would be to zip the questions and answers list and get 2-element tuple:

class Quiz:
    def __init__(self, quiz_file):
        self.quiz_file = quiz_file
        self.questions = []
        self.answers = []
        with open(self.quiz_file, 'r') as f:
            for i, line in enumerate(f):
                if i%2: # this is answer
                    self.answers.append(line.strip())
                else: # this is question
                    self.questions.append(line.strip())
                    
    def list_qa(self):
        for index, question, answer in enumerate(zip(self.questions, self.answers), start=1):
            print('Q{}: {}'.format(index, question))
            print('A{}: {}'.format(index, answer))
            print()
I will stop here for now and leave to you to try figure for yourself how to get single question/answer pair from it.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  quiz game - problem with loading statements from file lapiduch 2 1,076 Apr-20-2023, 06:13 PM
Last Post: deanhystad
Question Why do these codes give different answers? pooyan89 5 2,488 Dec-15-2020, 06:54 PM
Last Post: DeaD_EyE
  Basic Quiz/Game searching1 10 5,680 Nov-18-2018, 03:58 AM
Last Post: ichabod801
  Two Questions Regarding My Trivia Game martin28 1 2,390 Aug-19-2018, 02:07 PM
Last Post: martin28
  Quiz Game Help (ASAP Please!) beginnercoder04 2 3,208 Apr-15-2018, 04:13 AM
Last Post: beginnercoder04
  Just some second choice answers help please ajaY 6 5,118 Apr-07-2017, 08:25 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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