Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8887381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:47:41+00:00 2026-06-14T21:47:41+00:00

Okay so basically, I need a bit of help with this very easy program

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:47:42+00:00Added an answer on June 14, 2026 at 9:47 pm

    You can achieve this in one of two ways:

    1. itertools.cycle

    c = itertools.cycle(options)
    for i in range(anyNumber):
        message = Text(Point(x,y), next(c))
        message.draw(win)
        y = y + 30
    

    2. modulus

    for i in range(anyNumber):
        message = Text(Point(x,y), options[i%len(options)])
        message.draw(win)
        y = y + 30
    

    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:

    options = ["Hello", "How", "Are", "You"]
    for i in range(4):
        for e,elem in enumerate(options):
            print("%d: %s" %(4*i+e, elem))
    

    OR

    options = ["Hello", "How", "Are", "You"]
    for e,elem in enumerate(itertools.chain.from_iterable(itertools.repeat(options,4))):
        print("%d: %s" %(e, elem))
    

    OR

    options = ["Hello", "How", "Are", "You"]
    for i in range(4):
        for j in range(len(options)):
            print("%d: %s" %(4*i+j, options[j]))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my code: http://pastebin.com/y0VHgRNs Okay Basically, What i need to do is make
Okay guys, basically the problem I'm having is this. I've been assigned to write
Okay I wasn't really sure how to word this question, but basically what I
Okay this question is very simple: I have a facebook page, and a website.
Okay, I feel a bit foolish for having to ask this but I guess
Alright everyone this is a bit of a complicated setup so if I need
Okay,I've been following this tutorial http://coenraets.org/blog/android-samples/androidtutorial/ Basically it gives me exactly what i need
Okay here's the question, using C# I need to create a program using a
Okay, so this is a very strange problem and it has a lot of
OKay, I will try to make this make sense lol. Basically I have 4

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.