I am writing a program that produce a frequency plot of the letters in a body of
ID: 3583289 • Letter: I
Question
I am writing a program that produce a frequency plot of the letters in a body of text. however, there is an error in my code that I can not spot it. am using python 2.7, and the error point at (line3 freqs[char]+=1). any ideas?(i just posted this question and he or she copied and paste my question as an answer, so please if u do not know it do not answer). there is my code: def letter_count(word,freqs,pmarks): for char in word: freqs[char]+=1 def letter_freq(fname): fhand = open(fname) freqs = dict() alpha = list(string.uppercase[:26]) for let in alpha: freqs[let] = freqs.get(let,0) for line in fhand: line = line.rstrip() words = line.split() pmarks = list(string.punctuation) words = [word.upper() for word in words] for word in words: letter_count(word,freqs,pmarks) fhand.close() return freqs.values
Explanation / Answer
see You are calling in your code
freqs[char]+=1
but with the char = '.' without initialized a value freqs['.']=0
thats why it is showing an error
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.