LIMITED TIME OFFER: 50% OFF OF PREMIUM WITH OUR ANNUAL PLAN (THAT'S $294 IN SAVINGS).
GET OFFER

Why this did not work?

Screen Link:
https://app.dataquest.io/m/351/cleaning-and-preparing-data-in-python/5/string-capitalization

My Code:

'''for row in moma:
    Gender = row[5]
    Gender = Gender.title()
    if not Gender:
        Gender = "Gender Unknown/Other"
    row[5] = Gender
uniquenationalities = []
for row in moma:
    Nationality = row[2]
    if Nationality not in uniquenationalities:
        uniquenationalities.append(row[2])
print(uniquenationalities)
for row in moma:
    Nationality = row[2]
    Nationality = Nationality.title()
    if not Nationality:
        Nationality = "Nationality Unknown"
    row[2] = Nationality'''

What I expected to happen:
I only added a frequency table on top of the exercise so instead of taking the info they gave me, I could inspect the data myself.

What actually happened:

 momalist (<class 'list'>)
- actual + expected

Is it because I used two for loops instead of one as in the answer? If so, can someone explain?

No, the issue is quite minor actually. It’s your code here -

if not Gender:
        gender = "Gender Unknown/Other"

That gender should instead be Gender. That single g was the problem as that variable is not used later otherwise.

Fixing it should run your code without issues.

Update: Looks like you modified your code in your post while I was testing it out. It now shows Gender instead of gender, so your code should run.

2 Likes

Hi, sorry I should have left a note on that.

Still did not work after changing the g for a G. Could it be some issue with the cache?

I pasted the solution and it did work, so I am kind of confused.

Thank you for your help!

Are you absolutely sure you didn’t make any other change?

Because I reset that particular mission for myself, and used the exact code that you have currently pasted above, and clicked Submit and the grader passed me.

For this mission, you may not need uniquenationalities. It might be a problem with the iteration over moma. If the link to the problem is correct, this is how I solved it. I think it might be a good idea to get your variables down first. gender and nationality for this instance.

for row in moma:
    gender = row[5]
    gender = gender.title()
    nationality = row[2]
    nationality = nationality.title()
    if not gender:
        gender = "Gender Unknown/Other"
        
    if not nationality:
        nationality = "Nationality Unknown"
    row[5] = gender
    row[2] = nationality

So I would like to explain how this exercise work with this piece of code. I checked and found that when an item is empty in moma, it is left as ''. So the if not gender code identifies it as False. When it is either Male or Female it sees it as True.

glist = [['Male', 'Johnson', '23'], ['Female', 'Sarah', '25'], ['', 'Mann', '30']]

for g in glist:
    gender = g[0]
    if not gender:
        print('Not Found')
    else:
        print('Found')

Output:
        Found
        Found
        Not Found

Wow, thanks. I was going to write a conditional statement like: if gender == ‘’
Thanks, once again.

1 Like