-4

i am really beginner and just starting programming with python since 2 weeks. here is my question i cant find anywhere or i cant figure out the solution. i want to see my result in terminal with multiple line , for example i want to calculate long for loop and the result show up this loop on terminal with very long a single line. is there any solution for this? and sorry for my bad english isnt my native language.

Code:

list = list(range(1,100))
for x in list:
    if x % 3 == 0 or x % 5 == 0:
        print(x, end=' ')

Output:

3 5 6 9 10 12 15 18 20 21 24 25 etc...

But i want this one:

3 5 6 9 10
12 15 18 20 21
Georgy
  • 12,464
  • 7
  • 65
  • 73

3 Answers3

0

You could add an if statement before you print, to determine whether you want to have end be a blank or a newline character.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0
x_list = range(1,100)
n = 0
for x in x_list:
    if x % 3 == 0 or x % 5 == 0:
        if n == 4:
           print(x, end='\n')
           n = 0
        else:
           print(x, end=' ')
           n = n + 1
Rajas Rasam
  • 175
  • 2
  • 3
  • 9
  • 3
    While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation. – SherylHohman May 31 '20 at 18:43
0

You can change your print statement to:

print(x)

If you would print some results in the same line, you can do a logic to print some characters inline and after some iteration print in next line.

list1 = list(range(1,100))
for num, x in enumerate(list1):
    if x % 3 == 0 or x % 5 == 0:
        print(x, end=' ')
    if num % 10 == 0:
        print()
Danizavtz
  • 3,166
  • 4
  • 24
  • 25