Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

updating dictionary dose not work in the loop

H=0
 R=0
 C=0
 His=0
 Adv=0
 A=0
dic_of_genres={"Horror":H,"Romance":R,"Comedy":C,"History":His,"Adventure":Adv,"Action":A}
n=int(input())
for i in range(0,n):
    x=input().split()
    print(x)
    for items in x:
        if items =='Horror':
            H=H+1
        elif items =='Romance':
            R+=1
        elif items =="Comedy":
            C+=1
        elif items =="History":
            His+=1
        elif items =="Adventure":
            Adv+=1
        elif items =="Action":
            A+=1
print(dic_of_genres) `

I can not understand why my dictionary doesn’t update in each loop. Could any body help me?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You don’t need to go over each item. Note that I also added a try except in case x have different elements than dic_of_genres

for i in range(0,n):
    x= [item.capitalize() for item in input().split()]
    for genre in set(x):
               try:
                     dic_of_genres[genre] += x.count(genre)
               except KeyError:
                     pass

print(dic_of_genres)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading