The first one is: A program that searches within a file and by user input for a word, and counts how many of this word in the file, and if its in the file or not.

Thats what i reached so far:

sword, line= 0, 0
filename= raw_input("please input file name: ")
textf = open(filename, 'r')
ask= raw_input("Input the word your looking for: ")
for lines in textf:
  if ask in textf:
    sword += 1
  continue
print  "number of", sword

The second is: Write a program that prints a histogram. The program will read data from a file, each line of the file contains a number from range 0-10. Your program must count the occurrences of each score and print the score and the count of the score. hint: use list thats stores the count for each possible score.

What i have reached so far is nothing, perhaps someone can help me with a lead so i can continue.

You got fooled by the instruction in the posting box. code-tag must be followed by code-tag, and similarly for icode and icode. That instruction fooled me too. Easiest to use the (code) button on the bar above. To restate your code:

sword, line= 0, 0
filename= raw_input("please input file name: ")
textf = open(filename, 'r')
ask= raw_input("Input the word your looking for: ")
for lines in textf:
    if ask in textf:
        sword += 1
        continue
print "number of", sword

This looks nearly ok to me.

  • There is one error: What if the line is "I would like to like you" and the word is "like". You would count 1, but you should count 2.
  • I am amused by the token sword which is not an appropriate english language choice (not wrong, just amusing)
  • The continue keyword is redundant here.
  • Do you want to find words that are the same except for capitalization? (should 'like' match 'Like'?). If so, you need to do something to handle that case.

Hint: You may wish to use line.split() and list's count() method.
Hint: You can use list comprehension to get lowercase words.
Hint: You can use the re module if you are up to regular expressions.

for lines in textf:
    if ask in textf:

Print "textf" before the "if" statement so you know what you are testing. Using logic like this, entering a word like "the" will find "the", "then, "either", etc. You might want to split the file input into words and check for equal condition with "ask".

Woops. I missed that until woooee echoed it. You want to test ask against line:

for line in textf:
    if ask in line: # not "if ask in textf:"
       # count that words in the line

Agree that you probably want to split the line into words (and I would do it without doing the test above: Either way, underneath, Python is walking the whole line, in one case looking for a match, in the other doing the split, so there is likely no (or not much?) time savings if you only split when a match is found)

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.