16 AnswersNew Answer
16 AnswersNew Answer
Nicola Esu , there is a 2-liner code that can do that, but you should present us your own attempt first.
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.
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
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.
for i in range(20): print(i,end=', ' if (i+1)%5 else '\n')
for i in range(0,20,5): print(i) This could move to next 5th numbers from the current one. 0 5 10 15
for i in range(20): flag = i+1 if flag%5==0 : print() else print (*text*,end=" "
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
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
why others methods?
the range function takes a minimum of two arguments but it can also take three
visph others methods?
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
sorry i made a mistake
this is the correct code print(list(range(0,35,5)))
Learn Playing. Play Learning
SoloLearn Inc.4 Embarcadero Center, Suite 1455
Send us a message