please tell me how can I add line break after every 5 numbers
i = 1
while i < 30:
print(i, end = ' ')
i += 1
Use:
i = 1
while i < 30:
print(i, end = '\n' if i % 5 == 0 else " ")
i += 1
Should post an example for clarity, this should do it
for i in range(1, 30):
print(i, end=' ')
if not i % 5:
print('\n')
Just use the modulo operator %
with an if
-block to check if i
is a multiple of 5:
i = 1
while i < 30:
print(i, end=' ')
if i % 5 == 0:
print()
i += 1
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
Try using the Modulo funktion, which is done with % in python.
i=1
while i<30:
print(i, end=' ')
if i%5 == 0:
print()
i += 1
the Modulo funktion returns the remainder of the division i/5, which is 0 for 0,5,10 and so on, so every 5 steps.
print('\\n') prints a newline
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.