Press J to jump to the feed. Press question mark to learn the rest of the keyboard shortcuts

Print for loop on same line x number of times

Hopefully the title wasn't confusing, but basically what I want to do is take the result of my for loops and print it like this:

0, 1, 2, 3, 4
5, 6, 7, 8, 9

So if I have a user input value of say 5 I can have my for loops print out 5 at a time.
I've been looking all over and I can't figure out how to do it.

71% Upvoted
What are your thoughts? Log in or Sign uplog insign up
level 1

print(msg, sep=‘’) prints a message without adding newline to the end of it.

level 2
Original Poster1 point · 5 months ago

I've tried that, that would take everything and print it out like:
0, 1, 2, 3, 4, 5, 6, 7, 8, etc, etc...
instead of:
0, 1, 2, 3, 4, 5
6, 7, 8, etc, etc...

level 3

Then do a blank print() to add newline

level 4
Original Poster1 point · 5 months ago

I want it to change dynamically based on user input, I can't just add in blank spaces. It could segment by 3 or 5 or 25 based on what the user has entered.
It could be:
1, 2, 3
4, 5, 6
or it could be:
1, 2, 3, 4, 5, 6, 7
8, 9, 10, 11, 12, 13, 14
or even:
1, 2, 3, 4,...25
26, 27, 28,...50
It all depends on the user input.

level 5
Comment deleted by user5 months ago(More than 6 children)
level 6
Original Poster1 point · 5 months ago · edited 5 months ago
def listTwo():
    second = [0,0]
    third = []
    moreUserInput = int(input("Enter a number between 3 and 9: "))
    if moreUserInput <= 3 or moreUserInput >= 9:
        print ("Invalid number.")
        return listTwo()
        print (second)
        print (third)
    for x in range (second[0], third[0]):
        for y in range (second[1], third[1]):
            print (x*y)

Basically line 15 is where I want it to print on 1 line in increments of what the user entered.

level 7

Lack of indenting is awful.

level 8
Original Poster1 point · 5 months ago

Yeah it's fixed now.

level 9
Comment deleted by user5 months ago(0 children)
level 10
Original Poster1 point · 5 months ago

Yes it is

level 1

In the for loop, check if the step that you are on is a multiple of the user input number. If it is, print on new line

level 1
1 point · 5 months ago · edited 5 months ago

Try this or this.

level 1

You could just use \n for a newline.

level 2
Original Poster1 point · 5 months ago

No, no I cant

More posts from the learnpython community
Continue browsing in r/learnpython
Community Details





Subreddit for posting questions and asking for general advice about your python code.

r/learnpython Rules
Posting only the project goal is not allowed.
Be polite.
Read the wiki for answers to common questions.
Don't ask easily searchable questions.
Code Hosting/Formatting

Post your code on these websites and include the link in your thread, or click on the button below to find out how to properly format code and include it in your submission text.

Helpful posting resources

Please check out few of these links to see how to properly ask a software development related questions.

Related Subreddits

363k members


52.1k members


17.1k members


2.0m members


974k members


180k members

Commenting Guidelines
  • Try to guide OP to a solution instead of providing one directly.

  • Provide links to related resources.

  • Answer the question and highlight side-issues if any exist.

  • Don't "answer and run", be prepared to respond to follow up questions.

  • Proofread your answers for clarity and correctness.

  • Be polite.

Posting Guidelines
  • Try out suggestions you get and report back.

  • SSCCE Keep your code Short, Self Contained, Correct (Compilable) and provide Example

  • Include the error you get when running the code, if there is one.

  • Ensure your example is correct. Either the example compiles cleanly, or causes the exact error message about which you want help.

  • Avoid posting a lot of code in your posts.

  • Posting homework assignments is not prohibited if you show that you tried to solve it yourself.

Cookies help us deliver our Services. By using our Services or clicking I agree, you agree to our use of cookies. Learn More.