Okay so basically, I need a bit of help with this very easy program I’ve threw together:
from graphics import *
def main():
win = GraphWin("Text Window", 400, 600)
options = ["Hello", "How", "Are", "You"]
x = 200
y = 20
for i in range(4):
message = Text(Point(x,y), options[i])
message.draw(win)
y = y + 30
main()
Don’t worry about the ‘graphics’ module. It’s part of John Zelle’s Python book.
The point of this is I need to loop my range for 5 instead of 4, however, because ‘options’ is in [i], this particular program above will pull:
0: Hello
1: How
2: Are
3: You
4: ???
However, if I change the 4 to a 5, it will look for a 4th item in the list however it doesn’t exist, so it will spit out an “IndexError: list index out of range”
What I want to achieve is, when the program reaches the end of the list, to loop back to the first (0) item in the list.
For example,
for i in range(8):
Would pull out:
0: Hello
1: How
2: Are
3: You
4: Hello
5: How
6: Are
7: You
I have looked through this site and found some tools which haven’t lead to any success, this includes the ‘enumerate’ function which I don’t think will help.
If someone can shed some light on how to do this, it will be very welcome!
Hope I can do this without a nested loop aswell if this is at all possible.
Thanks you in advance for your help.
You can achieve this in one of two ways:
1.
itertools.cycle2. modulus
EDIT:
From the conversation in the comments:
If you want to print the contents of the list 4 times here are a couple of ways to accomplish this:
OR
OR