Python Forum

Full Version: New to python, starting from scratch
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
questions = ["What is your name?", "What is your favorite color?", "What is your quest?"]
n = 0
while True:
    print("Type q to quit")
    answer = input(questions[n])
    if answer == "q":
    n += 1
    if n > 2:
        n = 0
abouv is a some code I don't get. If I remove all the questions but one, this code give the following error:

line 5, in <module> answer = input(questions[n]) IndexError: list index out of range
I cannot find what is the matter Wall
The code is made to work with a specific amount of items in the list questions
The error happens because the list only has an item at index 0.
When it goes through the while True loop the first time n has the value 0.
questions[n] returns the first item of the list.
it continues to n += 1, n now has the value of 1.
It goes through the while True loop again and tries to call questions[n] to get the next item at index 1.
Because there isn't another item it gives the IndexError: list index out of range error.