Most common name

Screen Link: https://app.dataquest.io/m/1010/conditional-statements-practice-problems/20/most-common-name

My Code:

import csv
file = open('dq_unisex_names.csv')
reader = csv.reader(file)
rows = list(reader)

estimated_number = 0.0
common_name = None

for row in rows[1:]:
    if float(row[1]) > estimated_number:
        estimated_number = float(row[1])
        common_name = row[0]
print(common_name)

What I expected to happen:
My code did not work until I had included this line in it:

‘’’
estimated_number = float(row[1])
‘’’

without this line the output was as follows:

  • actual + expected

  • Aeon

  • Casey

Please can you explain why this row had to be included for the code to execute correctly?

Welcome to Dataquest Community @gnev80s

A suggestion and question together - have you tried printing the common_name variable while executing the loop? As in for every row, try to print common_name variable and see the result with the following code:

if float(row[1]) > estimated_number:
        # estimated_number = float(row[1])
        common_name = row[0]
        print(common_name)

What do you observe? Let us know.

You have initialized the estimated_number variable with 0. If you don’t assign it a value, with each execution of the loop, don’t you think you will be comparing the estimated_number of each row with 0 all the time!?

You need this line in order to update the estimated_number value when you find a more frequent name. Without this line you would always compare the name frequency to 0, so that the if statement would be True always and the output of your code would be the name from the las row.

Hi @Rucha, following your advice I tried printing the common_name variable while executing the loop. I got all the names and I thought I understood. However, when looking up the proposed solution…

maximum = None
max_name = None
for row in rows[1:]:
    if maximum == None or float(row[1]) > maximum:
        maximum = float(row[1])
        max_name = row[0]

print(max_name)

… I got confused. I thought that if I run the following code, I would get the same list of names as the code you proposed above.

maximum = None
max_name = None
for row in rows[1:]:
    if float(row[1]) > maximum:
        maximum = float(row[1])
        max_name = row[0]
print(max_name)

So:

  1. I did not understand what you explained in the first place. What did I get wrong?
  2. In the second code above, I’ve got: TypeError: ‘>’ not supported between instances of ‘float’ and 'NoneType’. How come the code works when if maximum == None or float(row[1]) > maximum but not if float(row[1]) > maximum?

Apologies for the long question.
thank you

Hi @boemer00

if float(row[1]) > maximum:

Here you are trying to compare something and if the comparison is true, it executes the block inside, ie the next 2 lines of codes.

In order to compare anything, they both should be of the same types, isn’t it?
Here on the left side you have a float value and on the right side you have maximum which is declared as None. So you are trying to comare a float number with None type, hence the error.

If you look at the other line of code

if maximum == None or float(row[1]) > maximum

there are 2 parts in the condition. if maximum == None or if float(row[1]) > maximum. Since maximum = None the line gets executed without causing an error.

Then it executes the lines of code within the if block which are

maximum = float(row[1])
max_name = row[0]

When this line gets executed the value of maximum gets updated with maximum = float(row[1]) even if you have set maximum as None or 0.0 previously.

So now, in the next iteration of the loop, the condition checking will happen against the updated value of maximum.

  1. So if you don’t declare the variable maximum outside the loop, with None or 0.0, it won’t be possible for the if condition to run.
  2. If you don’t assign a new value for maximum and update it, the if condition will always be checking against the value that has been assigned to it outside the loop, in this case either None or 0.0 whichever you are going to assing.

In case you are assigning maximum = None, you will need the extra condtion for your if condition. ie: if maximum == None or float(row[1]) > maximum:

In case you are assinging maximum = 0.0, then if float(row[1]) > maximum:
is enough.

If you assign maximum = None, and use if float(row[1]) > maximum: you will get the error message since you are comparing float against None.

I hope this clarifies your doubt.

4 Likes

Amazing, thank you so much for the detailed explanation. It helped me understand even other questions.

2 Likes