Practice Mode, Functions Intermediate, Page 6/8

Screen Link:

My Code:

# provided input
values = [10, 20, 30, 10, 30, 10] 
# answer to this input: 3, 1

def most_least_frequent(values):
    freq = {}
    for x in values:
        if x in freq:
            freq[x] += 1
        else:
            freq[x] = 1
            
    max_freq = None
    min_freq = None
    
    for i in freq:
        if max_freq is None or freq[i] > max_freq:
            max_freq = freq[i]
        elif min_freq is None or freq[i] < min_freq:
            min_freq = freq[i]
            
    return max_freq, min_freq

print(most_least_frequent(values))

What I expected to happen:

What actually happened:
The output was still correct, but this came up as well - I can see that when I change the elif to if this message doesn’t occur, but I don’t understand why, please can someone help?

"Your function returned an incorrect answer. Input: [['1', '1', '1', '1', '1', '1', '1']] Your answer: 7, None Expected answer: 7, 7"

Hi gnev80s

Welcome back!
The answer to input [10, 20, 30, 10, 30, 10] is (3, 1)

For input ['1', '1', '1', '1', '1', '1', '1'], the dict is {'1': 7}. Since 7 is the only frequency here, the answer to this input should be (7, 7).

As per your code

For dictionary {'1': 7}, max_freq will be assigned with the value 7 and then cursor will exit the loop. So max_freq will be 7 and min_freq will be None.
And this answer is not correct.
When you change the elif to if, that statement also gets executed and min_freq gets the value 7.

Hope this helps.
Thanks.