3

I hope you are doing well. I'm working on a dictionary program:

strEntry = str(input("Enter a string: ").upper())
strEntry = strEntry.replace(",", "")
strEntry = strEntry.replace(" ", "")

print('')

def freq_finder(strFinder):
  dict = {}
  for i in strFinder:
    keys = dict.keys()
    if i in keys:
      dict[i] += 1
    else:
      dict[i] = 1
  return dict

newDic = freq_finder(strEntry)

print(newDic)

newLetter = str(input("Choose a letter: ").upper())

if newLetter in newDic.values():
  print("Number of occurrence of", message.count(newLetter))
  newDic.pop(newLetter)
  print("Dictinary after that letter removed:", newDic)
else:
  print("Letter not in dictionary")

sortedDic = sorted(newDic)
print(sortedDic)

Everything works fine before this part:

newLetter = str(input("Choose a letter: ").upper())

if newLetter in newDic.values():
  print("Number of occurrence of", message.count(newLetter))
  newDic.pop(newLetter)
  print("Dictinary after that letter removed:", newDic)
else:
  print("Letter not in dictionary")

I'm trying to figure out how to check whether the letter is in the dictionary. If it is not, display the message “Letter not in dictionary”. Otherwise, display the frequency count of that letter, remove the letter from the dictionary and display the dictionary after that letter has been removed.

It should look something like this:

Enter a string: Magee, Mississippi

Dictionary:  {'M': 2, 'A': 1, 'G': 1, 'E': 2, 'I': 4, 'S': 4, 'P': 2}
Choose a letter: s
Frequency count of that letter: 4
Dictionary after that letter removed:  {'M': 2, 'A': 1, 'G': 1, 'E': 2, 'I': 4, 'P': 2}
Letters sorted: ['A', 'E', 'G', 'I', 'M', 'P']

I would highly appreciate it if you could tell me what's wrong and how to fix it.

MBahreman
  • 55
  • 5
  • 1
    You are checking if its in the values not in the keys. Instead of `if newLetter in newSic.values()` you want `if newLetter in newDic` – joshmeranda Sep 24 '21 at 03:39
  • Does this answer your question? [Check if a given key already exists in a dictionary](https://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary) – Joe Sep 24 '21 at 03:40
  • Are you trying to check if the value is in the dictionary as a key, value, or both? Because those are different problems. – BTables Sep 24 '21 at 03:40

1 Answers1

2

Check for the keys, not the value (Because the Value is a number, not a letter) -

if newLetter in newDic:  # Or if newLetter in list(newDic.keys())
  print("Number of occurrence of", message.count(newLetter))

For this - Dictionary: {'M': 2, 'A': 1, 'G': 1, 'E': 2, 'I': 4, 'S': 4, 'P': 2}, you could use Collections.Counter instead

PCM
  • 2,881
  • 2
  • 8
  • 30
  • It actually did work. I removed .values(). Thank you I will do my research on Collections.Counter – MBahreman Sep 24 '21 at 03:46
  • I just checked, and my "Number of occurrence of" also does not work. Do you have any ideas how I could fix it? – MBahreman Sep 24 '21 at 04:03