Help with understanding code on a quiz(entry-level beginner coder in need)

Hi there,

I am taking a quiz and one of the questions asked what the output of the code included below would be:

dictionary = {
    'one': 'two',
    'three': 'one',
    'two': 'three'
v = dictionary['one']

for k in range(len(dictionary)):
    v = dictionary[v]

I know that the answer is: two

But, I am unsure how we came up with that result.

I know that this line: v = dictionary[‘one’]
equals two but the for loop that follows it confuses me and im assuming that the answer to this question is two because the for loop didn’t change the original value of the first instance of v

Can someone explain to me what exactly is going on in this code?

I really appreciate any help,

Thank you! :blush: :innocent:

The for loop is changing v. Try putting some print statements inside the loop to show you what’s going on:

for k in range(len(dictionary)):
    print(f'Before, v is {v!a}')
    v = dictionary[v]
    print(f'After, v is {v!a}')
1 Like