26.7k views
1 vote
Declare a 4 x 5 list called N.

Using for loops, build a 2D list that is 4 x 5. The list should have the following values in each row and column as shown in the output below:

1 3 5 7 9
1 3 5 7 9
1 3 5 7 9
1 3 5 7 9
Write a subprogram called printList to print the values in N. This subprogram should take one parameter, a list, and print the values in the format shown in the output above.

Call the subprogram to print the current values in the list (pass the list N in the function call).

Use another set of for loops to replace the current values in list N so that they reflect the new output below. Call the subprogram again to print the current values in the list, again passing the list in the function call.

1 1 1 1 1
3 3 3 3 3
5 5 5 5 5
7 7 7 7 7

User Bob
by
7.5k points

1 Answer

5 votes

Answer:

# Define the list N

N = [[0 for j in range(5)] for i in range(4)]

# Populate the list with the initial values

for i in range(4):

for j in range(5):

N[i][j] = 2*j + 1

# Define the subprogram to print the list

def printList(lst):

for i in range(len(lst)):

for j in range(len(lst[i])):

print(lst[i][j], end=' ')

print()

# Print the initial values of the list

printList(N)

Output
1 3 5 7 9

1 3 5 7 9

1 3 5 7 9

1 3 5 7 9

--------------------------------------------------------------------

# Update the values of the list

for i in range(4):

for j in range(5):

N[i][j] = 2*i + 1

# Print the new values of the list

printList(N)

Output

1 1 1 1 1

3 3 3 3 3

5 5 5 5 5

7 7 7 7 7

Step-by-step explanation:

User James Lim
by
7.7k points

No related questions found