Dictionary and repetition structure

1

The problem asks me to count the size of holes in the text exp: A, D, R, O, P has only one hole, and B has two I made the following code:

qnt = int(input())

cont = 0

l = []

for i in range(qnt):

    txt = input()


dic = {'A':1, 'D':1, 'O':1, 'P':1, 'R':1, 'B':2}

for i in txt:

    if dic.get(i, 0):

       cont += dic[i]

print(cont)

However, only the number of holes in the last text appears, and I need each text.

    
asked by anonymous 24.12.2017 / 19:19

1 answer

0

First let's understand why you are only returning the last amount. Considering the code that is here, in the 5 line you have a list named l , but do not use it anywhere else.

In the 7 line you retrieve user input, and do not store it in a list, so the last entry will always be the value of txt .

...
5 l = []
6 
7 for i in range(qnt):
8 
9     txt = input()
...

I believe that what you were trying to do was add the value of txt in the l

...
5 l = []
6 
7 for i in range(qnt):
8 
9     txt = input()
10    l.append(txt)
...

If you do this, you would have to change the line 15 :

15 for i in txt:

To:

15 for i in l:

But you can leave it more Pythonic .

dic = {'A':1, 'D':1, 'O':1, 'P':1, 'R':1, 'B':2}
cont = 0
for i in range(int(input())):
  txt = input()
  if txt in dic: cont += dic[txt]

print(cont)
  

Example working on repl.it .

    
26.12.2017 / 00:24