0
def take_info(arr2,arr, number):

     arr[0] = number
     name = input()
     arr[1] = name
     grade = int(input())
     arr[2] = grade
     second_grade = int(input())
     arr[3] = second_grade
     arr2. append(arr)
    return arr2

def main():
     number = int(input())
     arr2 = []
     while number>0:
           arr = [[0] for _ in range(4)]
           arr2 = take_info(arr2,arr,number)
           number = int(input())

main()

take_info function must return while ' number >0 ', when the number is 0 or less than 0 loop would end. And I want to add these values and name to a list and the other list's each element should be that list. These code is working pretty well but I want to turn this code into something usefull.

Point 1 I am thinking is : how can I add that DIFFERENT Type of inputs into "arr"

Point 2 I am thinking is : When I am creating a list that like arr = [0] * 4, output gives me same value and exactly each value is the last one. Why is this happening can anybody tell me?

Barmar
  • 741,623
  • 53
  • 500
  • 612
Archie
  • 15
  • 7
  • If the code works and you're looking for advice on improving it, [codereview.se] is the appropriate place. But see https://codereview.meta.stackexchange.com/questions/5777/a-guide-to-code-review-for-stack-overflow-users first. – Barmar Apr 26 '21 at 22:19
  • A list of dictionaries would be more appropriate than a list of lists. – Barmar Apr 26 '21 at 22:19
  • Note: the question in _Point 2_ would not be [on-topic on CR](https://codereview.stackexchange.com/help/on-topic) – Sᴀᴍ Onᴇᴌᴀ Apr 26 '21 at 22:22
  • 1
    I don't understand your second point. You would have that problem if you wrote `[[0] * 4] * 3`. See https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly – Barmar Apr 26 '21 at 22:22
  • Or if you assigned `arr` outside the loop. – Barmar Apr 26 '21 at 22:23
  • Thanks for the recommend about codereview its going to be helpful, yes when ı assign arr outside the loop it gives me same values on output, When I look into link that you gave me, he is also doing like "for _ in range(x)]" he's doing it 2d list but ım doing 1d. But the main idea here is for _ in range(x) would give me the different values of elements--> [x,x,x] , – Archie Apr 26 '21 at 22:43
  • if I want to add that list to another list its going to be arr2 =[ [x,x,x] ], and ım going to change that x x x values to y y y value, arr --> [y,y,y] and add this list to arr2 again, arr2 = [ [x,x,x] , [y,y,y] ] it must be like this right ? but when ı assign it out of the loop each value is going to be last one, in this section[ [y,y,y] [y,y,y] ], why is this happening ı dont even understand – Archie Apr 26 '21 at 22:43

0 Answers0