Easy to read functin

Hi all, i try to copy the instruction when i work on easy to read function…
why below format does not work?

gender={}
for row in moma:
    genders=row[5]
    if genders in gender:
        gender[genders]+=1
    else:
        gender[genders]=1
print(gender)



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

Code indentation is improper from the way I am seeing the code.
Is the above code you wrote in the coding window? Kindly share the screenshot if not

Just remove the n and g variable from the braces. Then the code would work fine. Your variables are not getting passed into the braces.
Kindly refer to the below code:
Capture

Hi! @candiceliu93, In addition to the @pablajaspreet94 reply, what you can do,
just define n and g in .format() i.e “your string here”.format(n=n,g=g)
you were getting an error because you declared n and g in string but you
didn’t define what values will go in n & g in .format(),
so the updated code will be:

gender={}
for row in moma:
    genders=row[5]
    if genders in gender:
        gender[genders]+=1
    else:
        gender[genders]=1
print (gender)

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

Further, you can also use f-strings if you are using Python version 3.6
and greater, you can read about it here.
Hope it helps!

Correct, we can run the code with different techniques like the one @harsh.raizada mentioned.

thank you for reply! @pablajaspreet94 if i want to keep this code but i want to format the number. how to do it?

You are talking about this I guess. You can follow the above code if you want to keep your code. This will instruct that n will go to n and g will link to g.

Hi @candiceliu93 can you please bit explain what do you mean by “format the number” ?