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:

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 open(file_name, mode) ... then read from there....

Thanks, I actually realized my problem, I was running the program
python lab1.py getty.txt
instead of
python lab1.py < getty.txt

It works like that....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.