Last answered:

14 Sept 2021

Posted on:

11 Sept 2021

0

Resolved: Import counters (Part II), Exercises, Question 6

Hello!

As I was trying to solve the problem, I ran into an issue.
Here is a piece of my code:

mast = ['Clubs', 'Diamonds', 'Spades', 'Hearts']
value = ['Ace','2','3','4','5','6','7','8','9','10','Jack','Queen','King']
aux_list = []
cards = {}
for v in range(len(value)):
    for k in range(len(mast)):
        cards[mast[k]] = aux_list
    cards[mast[k]] = aux_list.append(value[v])
print(cards.items())

This is the result I get:

dict_items([('Clubs', ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']), ('Diamonds', ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']), ('Spades', ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']), ('Hearts', None)])

When I put the following piece of code at the end of the first loop right before printing the dictionary, it seems to fix the issue and helps me to recieve the needed result:

cards['Hearts'] = aux_list

but it looks like a crutch to me, I'm pretty sure there's a more ellegant way to fix my error.
What am I doing wrong? Why does the value of a key 'Hearts' turns into None?

Thank you in advance

2 answers ( 1 marked as helpful)
Instructor
Posted on:

14 Sept 2021

0

Hey Dmitriy,

Thank you for reaching out and for learning and practicing new skills with us!

To answer your question, let me draw your attention to the second for-loop in your code more specifically, the k iterator. The last thing that happens in the for-loop is that k becomes equal to three and that aux_list is appended to cards[mast[k]]. After this operation is completed, the following line is executed:

cards[mast[k]] = aux_list.append(value[v]).

While executing it, the value of k is still equal to three. Therefore, the code above corresponds to

cards[mast[3]] = aux_list.append(value[v])

which, in turn, corresponds to

cards['Hearts'] = aux_list.append(value[v]).

Now, note what you have on the right-hand side of this expression. Try and modify your code in the following way:

for v in range(len(value)):
    for k in range(len(mast))
        cards[mast[k]] = aux_list
    print(type(aux_list.append(value[v])))
    cards[mast[k]] = aux_list.append(value[v])
print(cards.items())

where we have added an additional print function. As you can confirm by executing the code, aux_list.append(value[v]) is of type NoneType. The reason for that is it is an operation – it doesn't bear any value. Therefore, every time you exit the second for-loop, you are assigning a NoneType to cards['Hearts']. This doesn't affect the
rest of the suits (Clubs, Diamonds and Spades) as they are handled inside the for-loop.

To obtain the desired result, modify your code in the following way:
for v in range(len(value)):
    for k in range(len(mast)):
        cards[mast[k]] = aux_list
    aux_list.append(value[v])
    cards[mast[k]] = aux_list
print(cards.items())

In order to truly excel in programming, practicing and studying the code are the way to go. I suggest you examine the original solution presented by Giles and compare to your own to see what the advantages and disadvantages of both are.

Good luck!

Kind regards,
365 Hristina

Posted on:

14 Sept 2021

0

Oooh, now I get it!
Thank you very much!

Submit an answer