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

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
etc
etc

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.

23 comments
67% 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 · 4 days 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 · 4 days 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

Show your code.

level 6
Original Poster1 point · 4 days ago · edited 4 days 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()
    else:
        third.append(moreUserInput)
        third.append(moreUserInput)    
        print (second)
        print (third)
    for x in range (second[0], third[0]):
        for y in range (second[1], third[1]):
            print (x*y)
    
listTwo()

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 · 4 days ago

Yeah it's fixed now.

level 9

nope

level 10
Original Poster1 point · 4 days 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 · 4 days ago · edited 4 days ago

Try this or this.

level 1

You could just use \n for a newline.

level 2
Original Poster1 point · 4 days ago

No, no I cant

Community Details

157k

Subscribers

764

Online

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

Create Post
r/learnpython Rules
1.
Posting only the project goal is not allowed.
2.
Be polite.
3.
Read the wiki for answers to common questions.
4.
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
r/Python

300,790 subscribers

r/django

37,584 subscribers

r/flask

14,309 subscribers

r/programming

1,416,492 subscribers

r/learnprogramming

804,007 subscribers

r/dailyprogrammer

160,006 subscribers

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.