Reputation: 57
I am trying to print 1 to 100 using while statement. Well, that is easy
n =100
i=0
while i<n:
i=i+1
print (i)
But the problem is how to put 1 to 10 in a row, 11 to 20 in a row, and finally to 91 to 100 in a row. Could you tell me the way?
Upvotes: 2
Views: 17865
Reputation: 27
x=1
while x<101:
if x%10==0:
print(" {}{}".format(x,"\n"),end="" )
else:
print(" {}".format(x),end="")
x+=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 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
Upvotes: 0
Reputation: 1
for i in range(100,-1,-1):
if i%10==0 and i!=100:
print(i)
else:
print(i,end=", ")
output:
100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90
89, 88, 87, 86, 85, 84, 83, 82, 81, 80
79, 78, 77, 76, 75, 74, 73, 72, 71, 70
69, 68, 67, 66, 65, 64, 63, 62, 61, 60
59, 58, 57, 56, 55, 54, 53, 52, 51, 50
49, 48, 47, 46, 45, 44, 43, 42, 41, 40
39, 38, 37, 36, 35, 34, 33, 32, 31, 30
29, 28, 27, 26, 25, 24, 23, 22, 21, 20
19, 18, 17, 16, 15, 14, 13, 12, 11, 10
9, 8, 7, 6, 5, 4, 3, 2, 1, 0
Upvotes: 0
Reputation: 3485
You can use zip
and iter
:
lst = [i for i in zip(*[iter(range(1,101))]*10)]
Change the last number to the size of the chunks you want; in this case it is 10
.
Output:
>>> lst
[(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, 30), (31, 32, 33, 34, 35, 36, 37, 38, 39, 40), (41, 42, 43, 44, 45, 46, 47, 48, 49, 50), (51, 52, 53, 54, 55, 56, 57, 58, 59, 60), (61, 62, 63, 64, 65, 66, 67, 68, 69, 70), (71, 72, 73, 74, 75, 76, 77, 78, 79, 80), (81, 82, 83, 84, 85, 86, 87, 88, 89, 90), (91, 92, 93, 94, 95, 96, 97, 98, 99, 100)]
Then print each number inside the list:
for i in lst:
for j in i:
print(j, end=" ")
print()
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 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
So overall you have:
lst = [i for i in zip(*[iter(range(1,101))]*10)]
for i in lst:
for j in i:
print(j, end=" ")
print()
EDIT:
As someone mentioned this only works in python 3. It can be done in python 2 by simply changing end=" "
to ,
:
lst = [i for i in zip(*[iter(range(1,101))]*10)]
for i in lst:
for j in i:
print j,
print
EDIT 2:
To do this with a while
loop:
counter = 1
n = 100
while counter < n+1:
if counter % 10 == 0:
print(counter)
else:
print(counter, end=" ") #Change this line to print counter, for python version 2
counter += 1
Upvotes: 4
Reputation: 1948
Your code is false. i is not I. indent is false too
def print_numbers(min_nu, max_nu, step):
import sys
if sys.version_info > (3,0):
my_xrange = range
else:
my_xrange = xrange
for i in my_xrange(min_nu, max_nu, step):
buff = [str(j) for j in my_xrange(i, i + step)]
print(" ".join(buff))
print_numbers(1, 100, 10)
Upvotes: 1
Reputation: 15953
You were pretty close, you were missing an if
statement and changing the end
for the print
statement to " "
as the default is '\n'
.
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
N = 20
I = 1
while I <= N:
if I % 10 > 0:
print(I, end = " ")
else:
print(I)
I += 1
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
Note:
I += 1
in python is simpler than doing I=I+1
. Also works for multiplication.
Python 2.x
The previous code works for Python 3.x as mentioned in the comments by @idjaw. For python 2.x, to use print
as a function rather than a statement (i.e. print()
) the followring needs to be imported at the start of the script.
from __future__ import print_function
Upvotes: 2
Reputation: 10951
A double nested for
loop should do the trick for u:
>>> for i in range(1,101,10):
for j in range(i, i+10):
print(j, end=' ')
print()
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 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
Or only using while
loop, if that's your requirement:
>>> i = j = 1
>>>
>>> while i < 101:
while j < i+10:
print(j, end=' ')
j += 1
i += 10
print()
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 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
Upvotes: 1
Reputation: 11
You can use this this should do the job:
def solution_01(n):
index = 0
row = ''
while index <=n:
index+=1
row += ' '+str(index)
if index%10 == 0:
print row
row = ''
solution_01(100)
Gives the 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 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
Upvotes: 1
Reputation: 9446
[[y for y in range(10 * x + 1, 10 + 10 * x + 1)] for x in range(10)]
Upvotes: 0