[Tutor] creat a program that reads frequency of words in file

Stephanie Quiles stephanie.quiles001 at albright.edu
Mon Jun 1 06:01:38 CEST 2015


Hello. i need serious help. i am a very very new python programmer. I have never done any code in my life. I am lost with these assignments for a class i am taking. I hope someone can assist. below is what i have so far which i know is incorrect. my question is how do i create a dictionary and save the words plus counts to it? i created an empty dictionary and i understand the program should read the entire file and create dictionary and store the data into it. but the only way i could get it to run at all was in the way you see below. i don’t think anything is actually being saved into the dictionary. i am so lost…


“”" Word Frequency

Write a program that reads the contents of a text file. The program should create a dictionary in which the
keys are the individual words found in the file and the values are the number of times each word appears.
for example, if the word 'the' appears 128 times, the dictionary would contain an element with 'the'
as the key and 128 as the value. the program should either display the frequency of each word or create a second file
containing a list of each words and its frequency.   """


def main():
    dict = {}
    count = 0
    text = input('enter word: ')
    data = open("words.txt").readlines()
    for line in data:
        if text in line:
            count += 1
    print("This word appears", count, "times in the file")



main()



More information about the Tutor mailing list