Loop Add List

How can I change this code to make 3 lists with 5 items in each, and not the way it is now. 3 listings with 5/10/15 items?

import random y = [] def autoSolve(): for i in range(5): z = random.randrange(1, 10) y.append(z) print(y, end="") for i in range(3): print("number", i + 1,) autoSolve() print() 
+6
source share
5 answers

Move y = [] to the autoSolve method so that it is reset for every call.

 def autoSolve(): y = [] for i in range(5): z = random.randrange(1, 10) y.append(z) print(y, end="") 
+10
source

You print the same list y every time.

y starts empty.

The first iteration of the for, y loop ends with 5 elements.

The second iteration of y.append causes it to grow to 10 elements.

To prevent this, put the line

 y=[] 

inside the autoSolve() method.

+1
source

Move y = [] to the beginning of autoSolve .

0
source
 import random y = [] def autoSolve(): x = [] for i in range(5): z = random.randrange(1, 10) x.append(z) print(x, end="") return x for i in range(3): print("number", i + 1,) y.append(autoSolve()) print() 
0
source

I think that would be a suitable solution for this problem.

 import random y = [] def autoSolve(): x = [] for i in range(5): z = random.randrange(1, 10) x.append(z) y.append(x) print(y, end="") for i in range(3): print("number", i + 1,) autoSolve() print() 

here the output will be generated, because it is [[], [], []] formate List with 3 internal list of 5 elements

-1
source

Source: https://habr.com/ru/post/894852/


All Articles