Home>
Contents

Store the number of arbitrary characters to be entered in the alphabet dictionary and store the number
"A is ●●
b is ●●
・ ・ ・
z is ●● pieces "
If the above "" is left as it is, all alphabets will be output, but I do not know how to output only the alphabets in which characters are counted.
I thought about making key and value independent like at the end of the code, but it didn't work. ..
How do I fix the code?
Thank you.

Corresponding source code
#Alphabet dictionary 2
import string
dic = {abc: 0 for abc in string.ascii_lowercase}
#print (dic)

#Input word list
input_words = []
while True:
    question = input ("Please enter English words:")
    if question == "":
          break
    input_words.append (question)

#Sort of input words
input_words_sort = sorted (input_words)
print ("Entered words:", input_words_sort)
#Alphabet count
for word in input_words:
    for abc in word:
        if abc in string.ascii_lowercase:
            dic [abc] + = 1
#print (dic)

Output how many #abc (word count num) are used
for key, num in dic.items ():
    print (f "{key}: {num}")
#for code in dic.keys ():
# print (code)
#for num in dic.values ​​():
# print (num)
  • Answer # 1

    If you just want to change the format of the output
    print (f "{key} is {num}")
    Is it like this?

  • Answer # 2

    numYou only have to put an if statement so that it prints only when is greater than 0.

    for key, num in dic.items ():
        if num>0:
            print (f "{key}: {num}")