| | |
Help for a novice python coder
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 6
Reputation:
Solved Threads: 0
I know this is a homework question, but I have reached the end of my rope and really need guidance. I have had four years of Java programming then walked into my CS 3 class and was given an assignment in python. I have never worked in Python before but tried writing some sample programs that seemed to work OK, but this project just won't cooperate. We have a text file(the Gettysburg Address) and we have to read it from sys.stdin into a dictionary which is to be sorted and have each word only once as the key with the word count for the value, then print out the dictionary. What I have is:
However when I run this(python lab1.py getty.txt) nothing happens. If I put a print statement at the top where I initialize the lists and dictionary, it gets printed, but no other print statements are reached. Is there something wrong with my code? Or is my computer just really slow at reading the text file? Any help for an absolute novice?
Python Syntax (Toggle Plain Text)
import sys word_dictionary = {} l = [] m = [] for s in sys.stdin: l = s.split() #split the line into a list of words for i in l: m.append(i) #add elements of l to m since l will be overwritten for j in m: j.strip(' ,.?!') j.lower() m.sort() for k in m: if k in word_dictionary: word_dictionary[k]=word_dictionary[k]+1 else: word_dictionary[k]=1 for word in word_dictionary: print word,word_dictionary[word]
However when I run this(python lab1.py getty.txt) nothing happens. If I put a print statement at the top where I initialize the lists and dictionary, it gets printed, but no other print statements are reached. Is there something wrong with my code? Or is my computer just really slow at reading the text file? Any help for an absolute novice?
You're never reading your file... you're literally trying to read from sys.stdin. This is wrong and I think you may have misunderstood your prof.
You need to open the file via
You need to open the file via
open(file_name, mode) ... then read from there.... Last edited by jlm699; Sep 2nd, 2008 at 11:40 pm.
![]() |
Other Threads in the Python Forum
- Previous Thread: Problem with name not defined
- Next Thread: Reloading a Python Script?
| Thread Tools | Search this Thread |
address alarm app beginner cipher cmd coordinates cx-freeze data decimals development dictionary directory dynamic error examples feet file float format function generator getvalue gui halp homework http images import input ip itunes java keycontrol leftmouse line linux list lists loop maintain maze millimeter module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pymailer pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation split sqlite ssh string strings sudokusolver terminal text thread threading time tlapse tooltip tuple tutorial ubuntu unicode url urllib urllib2 variable variables ventrilo vigenere web webservice wikipedia wx.wizard wxpython xlwt






