0

The goal of my program is to have the computer ask a user questions, and return some specifications for a computer that would fit their needs. Right now I am working on QuestionAsker, which is responsible for, as the class name suggests, asking the user questions. I've been hung up on the 4th line of AskQuestion() function. Before I tell you the problem, take a look at the code:

from question import Question

class QuestionAsker():
    questions = [
        Question("At minimum, what should your game be running on?", ["Low", "Medium", "Ultra"]),
        Question("On a scale of 1-3, how much flair do you want on your computer?", ["Low", "Medium", "Ultra"]),
        Question("Money doesn't grow on trees. How much money is in your budget?", ["$500", "$1000", "$2000+"]),
        ]

    index = 0   
    def AskQuestion(self):
        userInputForQuestion = raw_input(self.questions[self.index].question + " ")

        if userInputForQuestion not in self.questions[self.index].answers:
            print("Try again.")
            self.AskQuestion()


        self.questions[self.index].selectedAnswer = userInputForQuestion

        self.index += 1;

    def resetIndex(self):
        self.index = 0

    def ReadQuestions(self):
        pass

I was testing this code by calling AskQuestion a couple of times (to loop through all the questions), and to make sure this code was tippy top, I supplied multiple answers that returned "Try again," as it should. The issue is that if I supplied more than 1 wrong answer to a question, but then if I answered correctly after multiple wrong answers, I would get the following error message:

IndexError: list index out of range

I immediately suspected the [self.index] of the self.questions[self.index], so I began printing out the index to the console. I thought that the issue was that the AskQuestion was magically incrementing the self.index, on the final line of the AskQuestion function, but no. It kept printing a consistent number, in the case of the first question, 0!

I'm at my wits end here, and the other questions I've seen on this haven't served much help. Hopefully you guys can help, thanks!

2
  • When asking about code that produces an Exception you should always include the complete Traceback in the question. Copy the Traceback and paste it in the question, then format it as code (select it and type ctrl-k) Commented May 13, 2018 at 3:33
  • Why is QuestionAsker a class? Please show how you are using it - minimal reproducible example. Commented May 13, 2018 at 3:39

1 Answer 1

1

Please take care that in your function body, when a wrong answer is given, the function does not end. It makes a recursive call. When that call ends, the index is still incremented. So the wrong answers still mess the index up.

You should end the function after the wrong call is made, for what you want to happen.

if userInputForQuestion not in self.questions[self.index].answers:
    print("Try again.")
    self.AskQuestion()
    return None

or use else.

if userInputForQuestion not in self.questions[self.index].answers:
    print("Try again.")
    self.AskQuestion()
else:
    self.questions[self.index].selectedAnswer = userInputForQuestion
    self.index += 1;

Also please note that using recursion in such a way is not very common. Which made you make the mistake anyway.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.