I want to print a list [1,2,3,4,5,6,7,8,9] as
1 2 3
4 5 6
7 8 9
Here is my code
for i,j in enumerate(list):
if i is not 0 and i % 3==0:
print()
else:
print(j,end=" ")
My result is
1 2 3
5 6
8 9
Can someone help explain why this happen and give me some advise?
You should print the list item j unconditionally instead of doing it only when you are not printing a newline:
l = [1,2,3,4,5,6,7,8,9]
for i,j in enumerate(l):
if i is not 0 and i % 3==0:
print()
print(j,end=" ")
You can do:
tgt=[1,2,3,4,5,6,7,8,9]
n=3
print('\n'.join([' '.join(map(str, sl)) for sl in [tgt[i:i+n] for i in range(0,len(tgt),n)]]))
Prints:
1 2 3
4 5 6
7 8 9
Related
i would like to write the program
when i input the number of 12
the program will print of digits ie.after '9' is '0'.
For example:
Enter the number: 12
output
0
1
2
3
4
5
6
7
8
9
0
1
x = input("Enter the number: ")
x = int(x)
for i in range (x):
print(str(i))
anyone can give an idea for me? thanks
Change this:
print(str(i))
To this:
print(i % 10)
I am trying to print
2
1 3
2 4 6
1 3 5 7
2 4 6 8 10
I am not sure how to do it ,I could use print (...) And write everything but that is just stupid.
Is there a better way to do this?
for i in range(0, 5):
for j in range(0, i+1):
if i % 2 == 0:
print((j+1)*2, end=' ')
else:
print((j*2)+1, end=' ')
print()
maybe you can try this:
>>> def pattern_gen(layer):
... for i in range(layer):
... pattern=''
... start=(i+1)%2+1
... end=(i+(i+1)%2+1)*2
... for j in range(start,end,2):
... pattern+=f'{j} '
... print(pattern)
...
>>> pattern_gen(5)
2
1 3
2 4 6
1 3 5 7
2 4 6 8 10
I tried various programs to get the required pattern (Given below). The program which got closest to the required result is given below:
Input:
for i in range(1,6):
for j in range(i,i*2):
print(j, end=' ')
print( )
Output:
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9
Required Output:
1
2 3
4 5 6
7 8 9 10
Can I get some hint to get the required output?
Note- A newbie to python.
Store the printed value outside of the loop, then increment after its printed
v = 1
lines = 4
for i in range(lines):
for j in range(i):
print(v, end=' ')
v += 1
print( )
If you don't want to keep track of the count and solve this mathematically and be able to directly calculate any n-th line, the formula you are looking for is the one for, well, triangle numbers:
triangle = lambda n: n * (n + 1) // 2
for line in range(1, 5):
t = triangle(line)
print(' '.join(str(x+1) for x in range(t-line, t)))
# 1
# 2 3
# 4 5 6
# 7 8 9 10
What I am trying to do below in my code is just print all the types of number formats available in right aligned manner.
def print_formatted(number):
for i in range(1, n+1):
width = len(format(i, 'b'))
print("{0:d} {0:o} {0:x} {0:{w}b}".format(i, w=len(format(i, 'b'))))
if __name__ == '__main__':
n = int(input())
print_formatted(n)
Input:
4
Expected Output:
1 1 1 1
2 2 2 10
3 3 3 11
4 4 4 100
But actual output:
1 1 1 1
2 2 2 10
3 3 3 11
4 4 4 100
The above code works fine if I give a static value in-place of 'w' but if I pass dynamic changing value it is not working as expected. What am I missing here
Thanks in advance for your help.
I need to modify a program the program 'x' so that it outputs the original grid, but each row and column is labeled with it's respective number. i.e. column 1 has a first, row 1 has a 1 first. I have figured out how to put numbering in front of the rows, but cannot figure out how to modify the program so that the columns are numbered.
'x'
import random
aGrid = []
SIZE = 10
for r in range (0,SIZE,1):
aGrid.append ([])
for c in range (0,SIZE,1):
element = random.randint(0,9)
aGrid[r].append (element)
for r in range (0,SIZE,1):
for c in range (0,SIZE,1):
print(aGrid[r][c], end="")
print()
Here is my attempt at getting numbering in front of the rows
import random
aGrid = []
SIZE = 11
for r in range (0,SIZE - 1,1):
aGrid.append ([])
aGrid[r].append (r )
aGrid[r].append (" ")
for c in range (0,SIZE + 1,1):
element = random.randint(0,9)
aGrid[r].append (element)
for r in range (0,SIZE - 1,1):
for c in range (0,SIZE + 1,1):
print(aGrid[r][c], end="")
print()
You don't need to change aGrid. Just print the row and column numbers in the print loop:
import random
aGrid = []
SIZE = 10
for r in range (SIZE):
aGrid.append ([])
for c in range (SIZE):
element = random.randint(0,9)
aGrid[r].append (element)
print(" ", end="")
for c in range (SIZE):
print(c, end="")
print()
for i, r in enumerate(range(SIZE)):
print(i, end="")
for c in range(SIZE):
print(aGrid[r][c], end="")
print()
Output:
0123456789
01958724133
17006217440
21488953544
35615572045
49849348546
54207744418
63316678723
76651582077
85713263320
91939404287
A version that starts counting from 1 and has some spaces for better readability:
import random
aGrid = []
SIZE = 10
for r in range (SIZE):
aGrid.append ([])
for c in range (SIZE):
element = random.randint(0,9)
aGrid[r].append (element)
print(" ", end=" ")
for c in range (1, SIZE + 1):
print(c, end=" ")
print()
for i, r in enumerate(range(SIZE), 1):
print('{:2d}'.format(i), end=" ")
for c in range(SIZE):
print(aGrid[r][c], end=" ")
print()
Output:
1 2 3 4 5 6 7 8 9 10
1 3 0 4 6 3 9 3 3 6 3
2 5 6 1 6 6 3 2 5 6 1
3 6 9 0 5 0 7 1 1 7 7
4 7 4 3 9 0 9 1 0 7 8
5 5 1 1 1 1 7 1 4 4 8
6 4 5 8 1 6 3 6 2 8 6
7 4 1 0 5 7 4 5 6 6 4
8 4 5 5 4 3 3 0 9 2 1
9 3 6 7 0 0 9 5 8 5 9
10 3 1 2 3 5 0 1 6 2 9