@virnuls/

Pangrams - Dictionary

Python

An alternative version of the OCR Coding Challenges task, using a dictionary to count the number of times each letter is used

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# blank dictionary used to count letters
alphabet = {}
# descriptive string for output
pangram = "is"
# add characters A to Z to the dictionary
for char in range(65,91):
  alphabet[chr(char)] = 0
# input the text to be checked
text = input("Enter some text: ")
# loop through the text a character at a time
for char in text.upper():
  # ignore non-letters
  if char >= "A" and char <= "Z":
    # increment counter for each letter
    alphabet[char] += 1
# output the frequencies
print("\nThe letters appear with the following frequencies:\n")
for char in alphabet:
  print(char+":", alphabet[char])
# what's is the lowest letter count equal to zero?
if alphabet[min(alphabet, key=alphabet.get)] == 0:
  pangram += "n't"
print("\n"+text,pangram,"a pangram")