Answered You can hire a professional tutor to get the answer.

QUESTION

i keep getting an error and the problem is the print counts what is the right way to print the key and the count for this program also if you see any...

i keep getting an error and the problem is the print counts what is the right way to print the key and the count for this program also if you see any other errors please point them out

import string #To get punctuation characters

def readfile(fname):

  '''Return contents of a text file as a lower-case string'''

  f = open(fname, 'r')    #Open the file for reading

  contents = f.read()     #Read the contents of the file as one long string

  f.close()          #Close the file

  for char in string.punctuation:     #Use string.punctuation to remove punctuation

    contents = contents.replace(char, ' ')

  return contents.lower()   #Return the string (converting it to lower case

def count_pronouns(word_list, pronoun_list):

  '''Returns a dictionary with words and their counts

    word_list is a list of words from the text file

    pronoun_list is a list of English pronouns'''

  my_dict = {}      #Start with an empty dictionary

  for word in word_list:   #For each word in word_list

    if word in pronoun_list:      #If the word is a pronoun then

      if word not in my_dict:  #If the word is not in the dictionary

        my_dict[word] = 1

      else:

        my_dict[word] += 1

  return my_dict      #Return the dictionary

def show_counts(pronoun_dict):

  '''Print prounouns and their counts; print only those with counts > 0

    pronoun_dict is the dictionary of pronouns with their counts'''

  key_list = list(pronoun_dict.keys())

  key_list = key_list.sort()      #Sort the list of keys

  for key in key_list:        #For each key in the key list

    if pronoun_dict[key] > 0:     #If the count is greater than 0

      print(pronoun_dict[key]):     #Print the key and the count

def main():

  text_filename = 'Data//gb.txt' #Count pronouns in this file

  pronoun_filename = 'Data//pronouns.txt' #List of pronouns

  gb = readfile(text_filename) #gb is the contents of the text file (a string)

  gb_words = gb.split() #Split file contents into a list of words

  pronouns = readfile(pronoun_filename)

  pronoun_list = pronouns.split()

  pronoun_dict = count_pronouns(gb_words, pronoun_list)

  show_counts(pronoun_dict)

main()

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question