lundi 16 juillet 2018

why did the value of item2 change?

why did the value of item2 changed after line 20 no where in the whole code have i redefined item2 so technically it should not change. is there any way to run the same code without changing the value of item2

import  random   

def one(lenght):
    a = []
    for i in range(length):
        a.append(1)
    return(a)

def two(parent):
    b = parent
    if random.randint(0,10)<11:
        index = random.randint(0,6)
        b[index] = 6
    return b

item1 = one(6)
print(item1)
item2 = item1
print(item2)
item3 = two(item1)
print(item2)

result that i am getting

[1,1,1,1,1,1]

[1,1,1,1,1,1]

[1,6,1,1,1,1]




Aucun commentaire:

Enregistrer un commentaire