0

I am learning Python, and having a hard time understanding variable scope. I built a list to store all the 52 cards in a deck of cards.

I am working on a second function that shuffles the cards into a random order w/ random. My problem is that the original list I built is OUT of the scope of the second function, so I cannot call that list and make a copy. I also want to use .extend not copy. How do I get this back in scope to be able to make a copy inside the second function?

Current OUTPUT: ERROR - ReportUndefinedVariable

Expected OUTPUT: a copy of my original list in a new function that I can then apply random to change the order of the deck.

Here is my code (still learning, so a lot of repetitive variables/lists/etc. Will refactor after I figure this out).

def create_deck():
    card_faces = []
    card_deck = [] 
    face = ['T', 'J', 'Q', 'K', 'A'] 
    suites = ['s', 'h', 'd', 'c'] 
    orig_list = [] 

    # run random through suites
    for i in range(2, 10): # --> THIS IS looping from 2-> 10 and appending it to card_faces.
        card_faces.append(str(i))

    for j in range(5): # --> this is looping through the 4 face cards TJQKA
        card_faces.append(face[j])

    for k in range(4): # --> this is looping through the 4 suites and then it adds the faces to the suites.
        for n in range(13):
            card = (card_faces[n] + suites[k])
            card_deck.append(card)
    
    for m in range(52):
        #print(card_deck[m])
        orig_list.append(card_deck[m])   
    #print(orig_list)
    print(card_deck)

Here is the new function I am creating to copy list:

def copy_lst(list):
    copy = []
    copy.extend(list)
    return copy

extended_copy = copy_list(orig_list)
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • Welcome to Stack Overflow. "I am working on a second function that shuffles the cards into a random order w/ random. My problem is that the original list I built is OUT of the scope of the second function, so I cannot call that list and make a copy." It's hard to follow the description. Where in the posted code is the "first function"? Where is the "second function"? Which list is "the original"? What do you mean by "call that list" (this is not proper terminology)? Where in the code does the error occur? What is the [complete](https://meta.stackoverflow.com/questions/359146) error? – Karl Knechtel Oct 11 '22 at 05:15
  • Oh, I think I see. `orig_list` is the list that you want to copy, and the problem is when trying to call `copy_lst` using that list, from *outside either* function. I am out of close votes today, but this is a duplicate of [How do I get a result (output) from a function? How can I use the result later?](/q/3052793). – Karl Knechtel Oct 11 '22 at 05:18
  • As an aside: please read [ask] and note well that this is **not a discussion forum**. We don't want any information about you, or any conversation about how you're still learning etc. We want a direct, clear question. – Karl Knechtel Oct 11 '22 at 05:19
  • @KarlKnechtel Thanks for the response. First Stack post. First function is def create_deck(): and sorry original list is just what I am considering it because I will be altering the list in my second function so the "original list" is defined as orig_list = []. That is the list that I am looking to make a copy of and use within my second function at the bottom def copy_lst(list). Hope this clears it up. – chase coogan Oct 11 '22 at 05:21

1 Answers1

0

You have created orig_list in your function create_deck. So that variable will only be accessible inside that function create_deck. You should return orig_list in the create_deck and use it

def create_deck():
    card_faces = []
    card_deck = [] 
    face = ['T', 'J', 'Q', 'K', 'A'] 
    suites = ['s', 'h', 'd', 'c'] 
    orig_list = [] 

    # run random through suites
    for i in range(2, 10): # --> THIS IS looping from 2-> 10 and appending it to card_faces.
        card_faces.append(str(i))

    for j in range(5): # --> this is looping through the 4 face cards TJQKA
        card_faces.append(face[j])

    for k in range(4): # --> this is looping through the 4 suites and then it adds the faces to the suites.
        for n in range(13):
            card = (card_faces[n] + suites[k])
            card_deck.append(card)
    
    for m in range(52):
        #print(card_deck[m])
        orig_list.append(card_deck[m])   
    #print(orig_list)
    print(card_deck)
    return orig_list

def copy_list(list):
    copy = []
    copy.extend(list)
    return copy

orig_list = create_deck()
extended_copy = copy_list(orig_list)


Origin
  • 1,182
  • 1
  • 10
  • 25