I am very new to Python 3 and want to generate a random grid 4x6 numbers. I've tried various ways, but end up with it printing on new lines, or printing 4x6 the same numbers on each row etc.
Some examples of code (I tried lots of various ways, but I have to retype them because I am using a virtualbox with linux to run the Python:
import random
def grid_maker(h,v):
for y in range(1, 5):
x = random.randint(1, 100)
grid = [[str(x) for _ in range(v)] for _ in range (h)]
grid[0][0] = "o"
return grid
print ('\n'.join(''.join(row) for row in grid_maker(6,5)))
output
o85858585
8585858585
8585858585
8585858585
8585858585
8585858585
desired output
82 2 30 9
45 65 54 14
23 4 32 42
8 84 40 80
43 23 5 62
23 43 3 5
Another example
for t in range(1, 5)
i = random.randint(1, 100)
grid = [[i], [i], [i], [i]]
from pprint import pprint
pprint(grid)
output
[[100], [100], [100], [100]]
[[1], [1], [1], [1]]
[[80], [80], [80], [80]]
[[12], [12], [12], [12]]
desired output
[[34], [12], [2], [15]]
[[64], [34], [53], [4]]
[[35], [34], [61], [33]]
[[34], [55], [64], [23]]
Another example
ar = []
for t in range(1, 5):
i = random.randint(1, 100)
ar.append(i)
for t in range(1, 7):
for i in range(1, 5):
print(ar[t-1], end='')
But this prints out 585858585555555559595959343434342222222288888888root@root:/home/user#
Thanks
Aucun commentaire:
Enregistrer un commentaire