mardi 29 septembre 2015

Elif unexpected indentation when trying to print out single line with total count

I have created a very simple function that counts the number of occurrences in a list, however, I cannot get one single line to print out the total number of occurrences because the elif part of my if statement creates an unexpected indentation in my code. I'm using the elif part to check whether the code entered is part of the list or not.

So for example, instead of getting:

2 entries(s) for uk found

I get:

1 entries(s) for uk found
2 entries(s) for uk found

Can anyone please help me out with a suggestion on how to correct this? Thanks a lot !

PS: This is just my third week coding, please take it easy on me guys.

def myList(list):
    countOccurrence = 0    
    for code in list:
        if code == countryCode:
            countOccurrence += 1              
            print str(countOccurrence) + ' entrie(s) for ' + str(countryCode) + ' found'            
        elif countryCode not in list:
            print 'Invalid Country Code'
            break      

list = ['ec','us','uk','ur','br','ur', 'ur', 'uk']

countryCode = raw_input('Please provide country code > ')
myList(list)

Aucun commentaire:

Enregistrer un commentaire