157k views
3 votes
9.3 code practice python

Write a program that creates a 4 x 5 list called numbers
. The elements in your list should all be random numbers between -100 and 100, inclusive. Then, print the list as a grid.

For instance, the 2 x 2 list [[1,2],[3,4]]
as a grid could be printed as:

1 2

3 4

Sample Output
-94 71 -18 -91 -78
-40 -89 42 -4 -99
-28 -79 52 -48 34
-30 -71 -23 88 59

Note: the numbers generated in your program will not match the sample output, as they will be randomly generated.

1 Answer

1 vote

"The program first creates a list of size 4x5 and assigns randomly generated numbers to its indices. It then prints the list in a grid format on the screen. Best of luck."

import random

#Create list as 4x5 and assign random values between -100 and 100.

list = [[random.randint(-100, 100) for j in range(5)] for i in range(4)]

#Print as grid.

for i in range(len(list)):

for j in range(len(list[0])):

print(list[i][j], end=" ")

print()

9.3 code practice python Write a program that creates a 4 x 5 list called numbers-example-1
9.3 code practice python Write a program that creates a 4 x 5 list called numbers-example-2
User Dewayne
by
8.2k points