Hello, as the title says I’m trying to write a function that will loop through a user input
filename to find every word that starts with a user input
letter. The file contents and letter are passed through the function as parameters. Then I’m making a dictionary with the key values pairs: {each word: occurrence num}
.
The program is a bit large to copy-paste here. So I’ve condensed it to:
def fisearcher():
filename = input("Enter filename: ") # using RoadNotTakenRobertFrost.txt
letter = input("Enter a letter: ") # assume input == 'f'/'F'
with open(filename, "r") as f:
fidata = f.read()
text = fidata.casefold().split() # I split contents into csv list to loop over
for words in text:
if words.startswith(letter.casefold()):
n = text.count(words)
d_wn = {words: n}
print(f"{words}: {n}") # outputs full dictionary same as print(d_wn)
# output:
{'far': 1}
{'fair': 2}
{'for': 2}
{'first': 1}
{'for': 2}
{'fair': 2}
{'frost': 1}
-
I need print these
key: value
pairs without braces.
I’ve triedenumerate(d_wn)
too, it’s close but itreturns
prefixedindex
numbers.
Triedfor k, v in d_wn:
print(f"{k}: {v}"}
Which outputs:error: too many items to unpack
Triedfor k, v in d_wn.items(): print(k, ":", v)
output same as I included infunction
(Also tried the * operator but there were issues there too that I can’t recall atm.) -
Words are being added for each occurrence. If you notice in the output, there are 2 occurrences of ‘for’ and ‘fair’ both in the file and the dictionary. I need the dictionary to include only one key for each word.
# desired output:
'far': 1
'fair': 2
'for': 2
'first': 1
'frost': 1
# apostrophes are fine, just need to remove the curly braces
Can someone help point me in the right direction here? I’ve been stuck on this for the last day smh. Thanks in advance!