Hi.
So, I'm coding this GUI into a sort of quiz. I'm not sure how to explain what I plan to do so I will write it out in a pseudo code. I know this isn't the most efficient way at all to do this, but I'm just focused on getting this thing to work.

def main():
"draw window"

def round1():
ask question1
if correct:
go to round2()
ask question2
if correct:
go to round2()


Maybe you can see what I'm doing here, but basically what I want to know is, where would I place "def round2()" so that the command in "round1()" will know where to go?

I'm not too sure about all these multiple functions defs so if you could please let me know what would be the easiest way to connect the two different functions, I would greatly appreciate it. Let me know if you need anymore information.

I think you should use object oriented approach

I made a fairly simple Quiz

class MyQuiz:
    def __init__(self, questions, answers):
        # both questions and answers should be a list/tuple of string
        self.questions = questions
        self.answers = answers
        
        self.currentround = 0

    def playround(self, roundno):
        # roundno is the round number, otherwise correspond with the index
        # of the question and answer list/tuple.
        
        # don't use round as variable name as it is a function
        return raw_input(self.questions[roundno] + " Your answer: ").lower() == self.answers[roundno].lower()
    
    def mainloop(self):
        while self.currentround < len(self.questions):
            if (self.playround(self.currentround)):
                self.currentround += 1
                print "Your answer is correct!"
            else:
                print "Your answer is wrong!"

        print "You have completed the quiz!"


if __name__ == "__main__":
    q = ("What year is this?", "What is world's number 1 search engine?", "What language is this quiz programmed in?")
    a = ("2010", "Google", "Python")
    newGame = MyQuiz(q, a)
    newGame.mainloop()


""" Sample OUTPUT from the above program:

What year is this? Your answer: 2010
Your answer is correct!
What is world's number 1 search engine? Your answer: yahoo
Your answer is wrong!
What is world's number 1 search engine? Your answer: google
Your answer is correct!
What language is this quiz programmed in? Your answer: c++
Your answer is wrong!
What language is this quiz programmed in? Your answer: python
Your answer is correct!
You have completed the quiz!
"""

Of course you can have multiple answers,that would be as simple as changing the == in the play round to "in" and remove the lower() at the end. You probably need to prepare them by putting the answer to lower case in the beginning, you can also have error check etc.

Adding new feature is just adding new stuff to the playround function at this point, unless you want GUI, which is a bitch and requires more coding, but it just drawing stuff in the playround and __init__ function, for example, changing the raw_input to getting input from a textbox etc.

Edited 6 Years Ago by ultimatebuster: n/a

Comments
nice

Oh wow, I didn't even think about that. I'm pretty much done with my program now but I can definitely use a lot of what you showed me. I appreciate it so much! Thanks ultimatebuster :)

This article has been dead for over six months. Start a new discussion instead.