How do I apply a for loop to a string in Python which allows me to count each letter in a word? The ultimate goal is to discover the most common letter.
This is the code so far:
print "Type 'exit' at any time to exit the program."
continue_running = True
while continue_running:
word = raw_input("Please enter a word: ")
if word == "exit":
continue_running = False
else:
while not word.isalpha():
word = raw_input("ERROR: Please type a single word containing alphabetic characters only:")
print word
if len(word) == 1:
print word + " has " + str(len(word)) + " letter."
else:
print word + " has " + str(len(word)) + " letters."
if sum(1 for v in word.upper() if v in ["A", "E", "I", "O", "U"]) == 1:
print "It also has ", sum(1 for v in word.upper() if v in ["A", "E", "I", "O", "U"]), " vowel."
else:
print "It also has ", sum(1 for v in word.upper() if v in ["A", "E", "I", "O", "U"]), " vowels."
if sum(1 for c in word if c.isupper()) == 1:
print "It has ", sum(1 for c in word if c.isupper()), " capital letter."
else:
print "It has ", sum(1 for c in word if c.isupper()), " capital letters."
for loop in word:
I know I can use the:
(collections.Counter(word.upper()).most_common(1)[0])
format, but this isn't the way I want to do it.