![]() |
New to python, starting from scratch - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: New to python, starting from scratch (/thread-33330.html) |
New to python, starting from scratch - Pythnoobvent1971 - Apr-16-2021 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": break n += 1 if n > 2: n = 0abouv is a some code I don't get. If I remove all the questions but one, this code give the following error: I cannot find what is the matter ![]() RE: New to python, starting from scratch - Yoriz - Apr-16-2021 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.
|