-1

This piece of Python code prints out the maximum age in the Age = {age:numberOfOccurence} dictionary. But because all the ages are already greater than zero, why does the code print out 50 as the maximum age?

Age = {45:3, 50:2, 25:1, 10:5, 15:10}
Oldest = 0

for MaxAge in Age:
    if MaxAge>Oldest:
        Oldest = MaxAge

print(Oldest)
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
CoolC
  • 1

3 Answers3

2

I commented the code and added some print statements to help you understand how it works.

Age = {45:3, 50:2, 25:1, 10:5, 15:10}
Oldest = 0 ## Initialize oldest with the minimum number

print('Initial Oldest Value: ', Oldest)

for MaxAge in Age: ## Iterating Age
    ## If age at current index is greater than oldest (which is 0 for first iteration)  
    if MaxAge>Oldest: 
        ## Update oldest with current age
        print('MaxAge: ', MaxAge, ' > Oldest: ', Oldest)
        Oldest = MaxAge
        print('New Oldest Value: ', Oldest)
        print('-'*10)


print('Final Oldest Value: ', Oldest)

Output

Initial Oldest Value:  0                                                                                                         
MaxAge:  25  > Oldest:  0                                                                                                        
New Oldest Value:  25                                                                                                            
----------                                                                                                                       
MaxAge:  50  > Oldest:  25                                                                                                       
New Oldest Value:  50                                                                                                            
----------                                                                                                                       
Final Oldest Value:  50  
Javapocalypse
  • 2,223
  • 17
  • 23
1

Because, every-time something is greater than zero, you make the code set the Oldest variable to that, then next is greater than previous, that will be assigned, and so on... all this are done from this if statement:

if MaxAge>Oldest:
    Oldest = MaxAge

If you take out that, and add something else for the for loop, it won't work as expected.

Note that @andreihondrari's comment.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
1

I'm not sure if you are expecting the code to do something else, or if it's that you don't understand why it works.

However, you could take a look at this question and answer: Iterating over dictionaries using 'for' loops

The reason the code works is that the for loop over a dictionary will iterate over the keys of the dictionary. You can see this if you add an extra print statement like this:

Age = {45:3, 50:2, 25:1, 10:5, 15:10}
Oldest = 0

for MaxAge in Age:
    print(MaxAge)
    if MaxAge>Oldest:
        Oldest = MaxAge

print(Oldest)

Then you should see:

45
50
25
10
15
50
Captain Whippet
  • 2,143
  • 4
  • 25
  • 34