Hello! Help needed: введите сюда код

 list = ['0', '1'] res = [] def a(a): list[0] = a return list def x(): for i in range(5): a(i) print(a(i)) res.append(a(i)) print(res) 

It is necessary that the res list contains in the finals a list of possible options for the list, which for some reason does not occur, although on print a (i) everything looks correct. I do not understand what's the matter. How do you need to correctly put the values ​​in the list res, in order to get [[0, '1'], [1, '1'], [2, '1'], [3, '1'], [4, 'one']]?

So, with this question sort of figured out, the decision was to determine the list inside the function. And if the task became more complicated, and you need to work 2 functions at once with this list? there is no way to do without a global variable, and this again returns to the beginning. Here is a corrected code and a new task)

 lst = ['0', '1'] def a(one): lst[0] = one return lst def b(two): lst[1] = two return lst def x(): rng = range(5) res = [] for i in rng: a(i) for j in rng: b(j) res.append(lst) print(res) x() 

    2 answers 2

    Your list is defined outside of the function a.

    Accordingly, each time you manipulate the list variable inside the 'a' function, this is the same list each time. To re-create a separate list each time, you need to define it inside a function:

     res = [] def a(a): list = ['0', '1'] list[0] = a return list def x(): for i in range(5): a(i) print(a(i)) res.append(a(i)) print(res) 

    Well and on trifles:

    1. list is a reserved keyword, you should not use it as a variable name. If the program were a bit bigger and more difficult, you would most likely get problems with it. To designate a certain nameless list, you can use the name "lst" or "list_".

    2. This line is generally "wonderful":

      def a (a):

    Do not use the same name for different entities - it is very confusing.

    1. Well, what you do with res is not very good. Try not to use global variables. In good code, all variables that you change inside a function (not a method) must be created inside this function.

    A good programmer for your task would create a res object, and make a function x as a method of this object.

     class Res(list): def x(self): for i in range(5): a(i) print(self) self.append(a(i)) print(self) def a(a): list = ['0', '1'] list[0] = a return list res = Res() res.x() 
    • Thank. Damn constantly amazed, how simple it all was when I find the answer) - Ayaks
    • Thank you for the detailed comment. In fact, this is not all code, this is a very simplified piece of a large script, with variable names of variables, without any classes, and so on. Here it was important for me to understand the principle. I would like to get an answer to this question: what to do if you need to work with several functions at the same time with the list, and write everything down to the long-suffering res? for example the function b, which would change the list [1]? - Ayaks
    • A class can have many methods. - Xander

    similar lists are created like this

     [[i, 1] for i in range(5)]