| | |
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 |
abrupt ansi anti apache approximation array assignment avogadro backend beginner binary book builtin calculator character code converter countpasswordentry curved customdialog dan08 dictionaries dictionary dynamic examples exe file float format function gnu graphics gui heads homework import inches input java launcher library line lines linux list lists loop mouse mysql mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame pysimplewizard python random recursion redirect scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable wordgame write wxpython xlib






