0

Using functions, I would like to copy all items from pool_list and put them as a different list in live_wall then shuffle live_wall only. My list does not get copied (or cloned I guess) nor does it get shuffled. What am I doing wrong ?

import random

pool_list = ["1m1","2m1","3m1","4m1","5m1","6m1","7m1","8m1","9m1"]

live_wall = []
dead_wall = []

def copyPool():
    live_wall = list(pool_list)


def shuffleWall():
    random.shuffle(live_wall)

copyPool()
shuffleWall()

print(live_wall)
print(pool_list)
print(len(pool_list))
print(len(live_wall))
Mathieu
  • 29
  • 1
  • 7
  • Does this answer your question? [Using global variables in a function](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function) – kaya3 Feb 28 '20 at 02:11

2 Answers2

1

When you use live_wall = list(pool_list) inside your function it's creating a local variable of the same name. If you want to assign to a variable outside the function's scope you need to use the global keyword.

Eg

def copyPool():
    global live_wall
    live_wall = list(pool_list)
Loocid
  • 6,112
  • 1
  • 24
  • 42
1

If you don't want to use a global variable

  pool_list = ["1m1","2m1","3m1","4m1","5m1","6m1","7m1","8m1","9m1"]

  live_wall = []

  def copyPool():
      live_wall = list(pool_list)
      return live_wall

  live_wall = copyPool()

  print(live_wall)
alchemist_
  • 90
  • 1
  • 7