how to make this program not count space as a letter and count uppercase and lowercase letters together as "one"
word = str(input())
list = []
listchcounter = []
for character in word:
if character not in list:
list.append(character)
listchcounter.append(0)
for character in word:
for index in range(len(list)):
if list[index] == character:
listchcounter[index] = 1
for index in range(len(list)):
print("'" list[index] "'",'appears',listchcounter[index],'times')
CodePudding user response:
Only changes are in line 1 the .lower() behind the input. And in line 6 the and character != ' ' . Then the code already works fine.
word = str(input().lower())
list = []
listchcounter = []
for character in word:
if character not in list and character != ' ':
list.append(character)
listchcounter.append(0)
for character in word:
for index in range(len(list)):
if list[index] == character:
listchcounter[index] = 1
for index in range(len(list)):
print("'" list[index] "'",'appears',listchcounter[index],'times')
CodePudding user response:
The comments by @dimitry contain the basic ways to address your stated question.
To exclude spaces you want to filter them out with a if character != ' '
.
To treat upper and lower cased letters as the same, you want to convert all characters to lowercase via character = character.lower()
.
Let's put these together:
word = "foOtball Timeout"
letters = [letter.lower() for letter in word if letter != " "]
print(letters )
Giving us:
['f', 'o', 'o', 't', 'b', 'a', 'l', 'l', 't', 'i', 'm', 'e', 'o', 'u', 't']
Now we can count them. I would personally use collections.Counter()
for this, but it is easy enough to do it by hand.
letter_counts = {}
for letter in letters:
letter_counts[letter] = letter_counts.get(letter, 0) 1
print(letter_counts )
Giving us the crux of what is required:
{'f': 1, 'o': 3, 't': 3, 'b': 1, 'a': 1, 'l': 2, 'i': 1, 'm': 1, 'e': 1, 'u': 1}
Now all that is left is to print the results out nicely:
for letter, count in letter_counts.items():
print(f"The letter \"{letter}\" appears {count} time(s) in \"{word}\"")
Giving:
The letter "f" appears 1 time(s) in "foOtball Timeout"
The letter "o" appears 3 time(s) in "foOtball Timeout"
The letter "t" appears 3 time(s) in "foOtball Timeout"
The letter "b" appears 1 time(s) in "foOtball Timeout"
The letter "a" appears 1 time(s) in "foOtball Timeout"
The letter "l" appears 2 time(s) in "foOtball Timeout"
The letter "i" appears 1 time(s) in "foOtball Timeout"
The letter "m" appears 1 time(s) in "foOtball Timeout"
The letter "e" appears 1 time(s) in "foOtball Timeout"
The letter "u" appears 1 time(s) in "foOtball Timeout"
Full code:
word = "foOtball Timeout"
letter_counts = {}
for letter in [letter.lower() for letter in word if letter != " "]:
letter_counts[letter] = letter_counts.get(letter, 0) 1
for letter, count in letter_counts.items():
print(f"The letter \"{letter}\" appears {count} time(s) in \"{word}\"")
or if you wanted to use collections.Counter
:
import collections
word = "foOtball Timeout"
letter_counts = collections.Counter(l.lower() for l in word if l != " ")
for letter, count in letter_counts.items():
print(f"The letter \"{letter}\" appears {count} time(s) in \"{word}\"")