mardi 3 octobre 2017

How can I make if code check all cases?

#Checking if a word is an isogram
from collections import Counter
def count_isogram(words, index):
    a=Counter(words[int(index)])
    d=words[int(index)]
    for (b,c) in a.items():
        if c >= 2:
            print(b,c)
            return(d+' is not an isogram')
        else:
            print(b,c)
            return(d+' is an isogram')

Hi, that is my code above. I'm trying to make a very basic isogram checker (an isogram is a word that doesn't have any repeated letters (dog, cat, bird, etc.). I've gotten the code mostly working but when I get to my if statement, it checks the first letter of each word to determine which return phrase to use. How do I make my code check each letter? For example the below image link demonstrates the problem (I don't have a high enough rep to post images yet):

example

You can see that the word in the first scenario: 'silly' (index of 1) is being run throuh the function but because there is only 1 S, it returns that the word is an isogram, when it is not. When running the word 'dude' (index of 1), because the first letter occurs in the word more than once it runs the correct return, however it is only because the first letter checked is the one that is repeated.

I've tried running c.all(), c.any(), and a few other operators but it does not work because c is an integer of only 1 value.

What can I change/add to make the code check all possible letters prior to running the return?

Aucun commentaire:

Enregistrer un commentaire