Next line? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Next line?

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

26th Jan 2021, 6:35 AM
Nicola
Nicola  - avatar
16 Answers
+ 5
Nicola Esu , there is a 2-liner code that can do that, but you should present us your own attempt first.
26th Jan 2021, 11:07 AM
Lothar
Lothar - avatar
+ 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.
26th Jan 2021, 11:37 AM
Lothar
Lothar - avatar
+ 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
26th Jan 2021, 12:23 PM
Lothar
Lothar - avatar
+ 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.
27th Jan 2021, 9:51 AM
Lothar
Lothar - avatar
+ 3
for i in range(20): print(i,end=', ' if (i+1)%5 else '\n')
26th Jan 2021, 6:44 AM
visph
visph - avatar
+ 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
26th Jan 2021, 8:01 AM
Shri Hari L
Shri Hari L - avatar
+ 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
26th Jan 2021, 11:41 AM
Abhiyantā
Abhiyantā - avatar
+ 1
for i in range(20): flag = i+1 if flag%5==0 : print() else print (*text*,end=" "
26th Jan 2021, 8:08 AM
Sadra Shakouri
Sadra Shakouri - avatar
+ 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
26th Jan 2021, 12:40 PM
visph
visph - avatar
0
why others methods?
26th Jan 2021, 7:07 AM
visph
visph - avatar
26th Jan 2021, 12:33 PM
Abhiyantā
Abhiyantā - avatar
0
the range function takes a minimum of two arguments but it can also take three
27th Jan 2021, 6:42 PM
Avalon
- 1
visph others methods?
26th Jan 2021, 7:05 AM
Nicola
Nicola  - avatar
- 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
27th Jan 2021, 6:44 PM
Avalon
- 1
sorry i made a mistake
27th Jan 2021, 6:47 PM
Avalon
- 1
this is the correct code print(list(range(0,35,5)))
27th Jan 2021, 6:47 PM
Avalon