Next line? | SoloLearn: Learn to code for FREE!

0

Next line?

for i in range (20): how can I tell to go to the next line every 5 numbers?

1/26/2021 6:35:25 AM

Nicola Esu

16 Answers

New Answer

+5

Nicola Esu , there is a 2-liner code that can do that, but you should present us your own attempt first.

+5

visph , when running your code with a range other than starting with 0, the result will not be as expected. the reason is, that the code uses the value coming from the range for the modulo division. e.g.: for i in range(3,22): -> will give und unexpected result. what can be done is to use an enumerator (in the for loop) together with the values from the range, and use these enumerator values for modulo division.

+5

Rishav , the code you presented has the same issue as i described in my 2nd post. read this carefully to understand the issue. the code you presented will print only 4 numbers in first line, then 5 numbers. running it with ... range(3,21), the result will be: ( i included a space for better readability: print(i, end=" ") 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

+5

visph , the request of the op was: "how can I tell to go to the next line every 5 numbers?" what i would mention is that the algorithm used (taking the numbers from the range and use these as a counter) is irresponsible following the the basic rules of coding. the range that is used in the code is from 0 to 20 (not included), but this could change. and a coder had to take care about this facts. so for me this is a no-go, and i see it as my responsibility, to mention this to the op.

+3

for i in range(20): print(i,end=', ' if (i+1)%5 else '\n')

+3

for i in range(0,20,5): print(i) This could move to next 5th numbers from the current one. 0 5 10 15

+2

for i in range(20): flag = i+1 if flag%5==0 : print() else print (*text*,end=" "

+2

for i in range(1,21): if i % 5 == 0: print() print(i, end="") Loop will run from 1 to 20 and if i is divisible by 5 then it will leave a line

+1

Lothar you're (almost) right about range not starting at zero (or five multiple)... as you stated, this could be easily fixed with an enumerator, but the OP question wasn't about this ;P

0

why others methods?

0

https://code.sololearn.com/ceifmY3S4jWj/?ref=app

0

the range function takes a minimum of two arguments but it can also take three

-1

visph others methods?

-1

so if I say for x in range (0,35,5): print i it will print every number between 0 and 35 per integer of 5

-1

sorry i made a mistake

-1

this is the correct code print(list(range(0,35,5)))