How do I use a list from a different function?

advertisements
cards1 = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v']
def piles(cards1):
    print("You must remember what card is yours!")
    pile = [[],[],[]]
    first = 0
    second = 1
    third = 2
    for i in range (7):
        pile[0].append(cards1[first])
        pile[1].append(cards1[second])
        pile[2].append(cards1[third])
        first += 3
        second += 3
        third += 3
    print(pile)
    return(pile)
piles(cards1)

def sorting_piles():

    sorted_piles = []
    final_pile = []
    which_pile = int(input("Which card is your pile in, 1,2 or 3?"))

    for i in range (2):
        while which_pile not in(1,2,3):
            which_pile = int(input("Invalid input. Which card is your pile in, 1,2 or 3?"))
        if which_pile == (1):
            sorted_piles.append(pile[2,0,1])
        elif which_pile == (2):
            sorted_piles.append(pile[2,1,0])
        else:
            sorted_piles.append(pile[1,2,0])
        print("This is now the new pile:",sorted_piles)

    for i in range(7):
        final_pile.append(sorted_piles[0][i-1])
    for i in range(7):
        final_pile.append(sorted_piles[1][i-1])
    for i in range(7):
        final_pile.append(sorted_piles[2][i-1])
    print("Your card is:",final_pile[10])
    return(final_pile)
sorting_piles()

Whenever I run this code, the first function runs perfectly and I am able to input which pile my card is in but after I input, I get this error message:

NameError: name 'pile' is not defined

How do I make the second function recognise the list 'pile'? Thanks


You have no variable that is catching the output being returned from the function definition. You need this :

pile = piles(cards1)