Python Data Analysis Basic 331-9

Screen Link:
https://app.dataquest.io/m/331/python-data-analysis-basics/9/challenge-summarizing-artwork-gender-data

My Code:


gender_freq = {}
for row in moma:
    gender = row[5]
    if gender not in gender_freq:
        gender_freq[gender] = 1
    else:
        gender_freq[gender] += 1
    

for gender, num in gender_freq.items():
    template = "There are {n:,} artworks by {g} artist"
print(template.format(g=gender, n=num))

What I expected to happen:
To give me right results = output for all Male, Female, and Unknown gender categories

What actually happened:

There are 13,492 artworks by Male artist

The output only showed one category. I wrote exact code as the “See the answer” but still the outcome came out different than expected answer. The same thing happened in earlier page as well. The output for only last row showed up for the outcome instead of all the rows. Yet, my answers were the same as “See the answer”. What did I do wrong?

1 Like

Hi @sangle2,

I would be happy to help you with it. This is the part of your code that you need to fix:

for gender, num in gender_freq.items():
    template = "There are {n:,} artworks by {g} artist"
print(template.format(g=gender, n=num))

print(template.format(g=gender, n=num)) should be inside the for loop. Otherwise, it will only be executed once,

for gender, num in gender_freq.items():
    template = "There are {n:,} artworks by {g} artist"
    print(template.format(g=gender, n=num))

Now the only issue is a typo in template = "There are {n:,} artworks by {g} artist". It should be artists.

for gender, num in gender_freq.items():
    template = "There are {n:,} artworks by {g} artists"
    print(template.format(g=gender, n=num))

Hope this helps :slightly_smiling_face:

Best,
Sahil

2 Likes

Hii @sangle2,
After seeing your code, I have the following comments:
Your entire code is correct but you have to keep the following print statement
print(template.format(g=gender, n=num))
inside the for loop. Then only u can print all rows. Hope this will fix your problem.

Kind regards,
Jyothsna bondada

2 Likes

Hi @Sahil,
I’m getting the same output when I put in the solution code and my answer, but the problem is that I seem to be getting an answer that is off of the output by a weird number. Could you please help me out?

Here’s my code:

1 Like

Hey.

This is probably an issue on our end. Please prepend the following code to your solution to hopefully fix it.

url = "https://dq-content.s3.amazonaws.com/331/artworks_clean.csv"

r = __import__("requests").get(url)

with open("artworks_clean.csv", "w") as f:
    f.write(r.content.decode("UTF-8"))

For more context on this read Length error with DQ exercise.

I hope this helps.

2 Likes

Hi @naidumsb,

Sorry about that! I am able to reproduce this issue on my end. I will get it logged. To mark the screen as complete, please follow the steps mentioned in this article:

Best,
Sahil

Any idea on what’s wrong with this?

Hi @ssrinivas1,

This could be an issue on our end. Please follow the instructions in this post to fix this issue:

Best,
Sahil

It does not help. It will still give the same!

1 Like

Hm… It’s weird, It is working fine on my end now. Previously I was able to reproduce it. In any case, I will report this issue to the engineering team.

If the following code didn’t work for you:

# WORKAROUND
url = "https://dq-content.s3.amazonaws.com/331/artworks_clean.csv"

r = __import__("requests").get(url)

with open("artworks_clean.csv", "w") as f:
    f.write(r.content.decode("UTF-8"))

# SOLUTION CODE
gender_freq = {}

for row in moma:
    gender = row[5]
    if gender not in gender_freq:
        gender_freq[gender] = 1
    else:
        gender_freq[gender] += 1

for gender, num in gender_freq.items():
    template = "There are {n:,} artworks by {g} artists"
    print(template.format(g=gender, n=num))

I would recommend you to follow this method to mark the screen is completed.

Best,
Sahil

1 Like

I was able to reproduce the same issue that ssrinivas1 had while going through the course just now. Just sharing for additional bug use case.

1 Like

Hi @la.cayabyab,

Thank you for letting us know. This issue is very difficult to reproduce, I have notified the team that this has occurred again. I will update this topic once the issue is fixed.

Best,
Sahil