Discussion:
[Tutor] creat a program that reads frequency of words in file
Stephanie Quiles
2015-06-01 04:01:38 UTC
Permalink
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()

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://
Alan Gauld
2015-06-01 09:56:08 UTC
Permalink
Post by Stephanie Quiles
Hello. i need serious help. i am a very very new python programmer.
Welcome.
Post by Stephanie Quiles
Write a program that reads the contents of a text file.
OK, Well you have done that bit OK.
Post by Stephanie Quiles
The program should create a dictionary in which the
keys are the individual words found in the file
But you are failing on this bit. As you said you are not storing
anything in the dictionary. You need to split your lines into individual
words, then store each word in the dictionary.

I suggest you simplify the problem for now and try just doing that.
Don't worry about counting them for now just save the words into the
dictionary. You should end up with the dictionary containing one entry
for each of the different words in the file.

Some other comments below.
Post by Stephanie Quiles
dict = {}
Don't use dict as a variable because thats a Python function for
creating dictionaries. By using it as a name you hide the
function. AS a general rule never name variables after
their type, name them after their purpose. In this case it could be
called 'words' or 'counts' or something similar.
Post by Stephanie Quiles
count = 0
text = input('enter word: ')
You weren't asked to read a word from the user.
All the data you need is in the file.
Post by Stephanie Quiles
data = open("words.txt").readlines()
You don't need the readlines() line you can just do

for line in open("words.txt"):

However, best practice says that this is even better:

with open("words.txt") as data:
for line in data:

This ensures that the file is correctly closed
at the end.
Post by Stephanie Quiles
count += 1
print("This word appears", count, "times in the file")
And this is, of course, completely off track. You need
to split the line into its separate words and store
each word into the dictionary.

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Thomas C. Hicks
2015-06-01 21:35:10 UTC
Permalink
Post by Alan Gauld
Post by Stephanie Quiles
count += 1
print("This word appears", count, "times in the file")
And this is, of course, completely off track. You need
to split the line into its separate words and store
each word into the dictionary.
OP may want to research the setdefault and get methods for dictionaries.

SDG,

tom
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-06-01 23:42:22 UTC
Permalink
I've CCd the list. Please use reply all when responding to the list.
Also please use plain text as HTML/RTF doesn't work on all
systems and code in particular often gets mangled.
Hello again,
here is the final code… I think :) please see below. Is this is the
easiest way to go about it? I appreciate your assistance!
words = {}
count =0
Do you need count? What is its purpose?
text = line.split()
words[word] =1
words[word] +=1
Look into the setdefault() method of dictionaries.
It can replace the if/else above.
count +=1
print(words)
Aside from the two comments above, good job!
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.pyth
Alan Gauld
2015-06-02 07:19:52 UTC
Permalink
Post by Alan Gauld
words[word] =1
words[word] +=1
Look into the setdefault() method of dictionaries.
It can replace the if/else above.
On reflection, the get() method might be even more useful.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Stephanie Quiles
2015-06-02 01:35:16 UTC
Permalink
thanks on the help. I am now stuck on this program for quizzing on state capitals. Do you mind taking a look please? I can’t get it to tell me the answer is incorrect it just keeps asking me for capitals whether the answer is right or wrong. It also is not giving me correct counts for correct and incorrect answers. Any help would be appreciated. Not sure if i turned HTML. my laptop is fairly new and I’m still assimilating to iOS. Please let me know if the code is hard to read.

Thanks

_______________________________________________________________________________
Write a program that creates a dictionary containing the U.S. States as keys and their
capitals as values.
(Use the internet to get a list of the states and their capitals.)
The program should then randomly quiz the user by displaying the name of a state and asking
the usr to enter that state's capital.
The program should keep a count of the number of correct and incorrect responses.
(As an alternative to the US states, the program can use the names of countries and
their capitals.)"""

import pickle


def main():
right = 0
wrong = 0
capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', \
\
"Arizona": 'Phoenix', \
\
'Arkansas': 'Little Rock', 'California': 'Sacramento', \
\
'Colorado': 'Denver', \
\
'Connecticut': 'Hartford', 'Delaware': 'Dover', \
\
'Florida': 'Tallahassee', \
\
'Georgia': 'Atlanta', 'Hawaii': 'Honolulu', \
\
'Idaho': 'Boise', \
\
'Illinois': 'Springfield', 'Indiana': 'Indianapolis', \
\
'Iowa': 'Des Moines', \
\
'Kansas': 'Topeka', 'Kentucky': 'Frankfort', \
\
'Louisiana': 'Baton Rouge', \
\
'Maine': 'Augusta', 'Maryland': 'Annapolis', \
\
'Massachusetts': 'Boston', \
\
'Michigan': 'Lansing', 'Minnesota': 'Saint Paul', \
\
'Mississippi': 'Jackson', \
\
'Missouri': 'Jefferson City', 'Montana': 'Helena', \
\
'Nebraska': 'Lincoln', \
\
'Nevada': 'Carson City', 'New Hampshire': 'Concord', \
\
'New Jersey': 'Trenton', \
\
'New Mexico': 'Santa Fe', 'New York': 'Albany', \
\
'North Carolina': 'Raleigh', \
\
'North Dakota': 'Bismarck', 'Ohio': 'Columbus', \
\
'Oklahoma': 'Oklahoma City', \
\
'Oregon': 'Salem', 'Pennsylvania': 'Harrisburg', \
\
'Rhode Island': 'Providence', \
\
'South Carolina': 'Columbia', \
\
'South Dakota': 'Pierre', 'Tennessee': 'Nashville', \
\
'Texas': 'Austin', 'Utah': 'Salt Lake City', \
\
'Vermont': 'Montpelier', \
\
'Virginia': 'Richmond', 'Washington': 'Olympia', \
\
'West Virginia': 'Charleston', \
\
'Wisconsin': 'Madison', 'Wyoming': 'Cheyenne'}

for k in capitals.keys():
state = input('Enter the capital of '+k+' :')
if state.upper() == capitals[k].upper():
right += 1
print('Correct')
else:
wrong += 1
print('Incorrect')
choice = input('Do you want to play again y/n: ')
if choice.upper() == 'N':
print('end of game')
else:
choice.upper() != 'Y'
print("invalid choice")

print('Number of correct answers is: ', right)
print("Number of incorrect answers is:", wrong)

main()
Post by Alan Gauld
I've CCd the list. Please use reply all when responding to the list.
Also please use plain text as HTML/RTF doesn't work on all
systems and code in particular often gets mangled.
Hello again,
here is the final code… I think :) please see below. Is this is the easiest way to go about it? I appreciate your assistance!
words = {}
count =0
Do you need count? What is its purpose?
text = line.split()
words[word] =1
words[word] +=1
Look into the setdefault() method of dictionaries.
It can replace the if/else above.
count +=1
print(words)
Aside from the two comments above, good job!
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
http://www.flickr.com/photos/alangauldphotos
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/t
Alan Gauld
2015-06-02 09:48:21 UTC
Permalink
Post by Stephanie Quiles
import pickle
You don;t need pickle
Post by Stephanie Quiles
right = 0
wrong = 0
capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', \
\
"Arizona": 'Phoenix', \
You don't need the \
Inside {} you can put newlines as much as you like:


'Arkansas': 'Little Rock', 'California': 'Sacramento',
'Colorado': 'Denver', 'Connecticut': 'Hartford',
'Delaware': 'Dover', 'Florida': 'Tallahassee',
etc...
Post by Stephanie Quiles
state = input('Enter the capital of '+k+' :')
This loop just keeps reading the inputs but does nothing with them.
I suspect you intended to have the following lines inside the loop?
Post by Stephanie Quiles
right += 1
print('Correct')
wrong += 1
print('Incorrect')
choice = input('Do you want to play again y/n: ')
print('end of game')
You need to provide a mechanism for exiting the loop.
The break statement can do that for you.
Post by Stephanie Quiles
choice.upper() != 'Y'
print("invalid choice")
print('Number of correct answers is: ', right)
print("Number of incorrect answers is:", wrong)
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Continue reading on narkive:
Search results for '[Tutor] creat a program that reads frequency of words in file' (Questions and Answers)
58
replies
Christians do you acknowledge that god created evil?
started 2018-06-26 19:15:30 UTC
religion & spirituality
Loading...