Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Basic Python programming help?

I need a python script that will count the number of a particular occurence in a file that i have. Can someone help with this?

2 Answers

Relevance
  • 7 years ago

    With no description of what the file looks like and what you're trying to count in it? Not likely.

    Someone would have to make a very lucky guess--and that sort of luck should be buying a lottery ticket rather than typing on Y!A.

  • 7 years ago

    You have 123abc123def123abc

    You are told to count how many 123's there are

    while not EOF (end of file) read the next char.

    Dose the char match 1? If not, go back to the top of the loop and read read next.

    Test for match, no, try again, test for match and Yes, we have a match

    We are now at the 5 element which we read in and then compare with '2'. We have a match. matches = matches + 1

    Move to next char which is the 6th element and compare it to 'C' and we have another match. We now have a full set of "abc"

    So now we go back up to the top of our loop again and read in char 5, compare against 1 ...

    Continue until EOF and then display match count.

Still have questions? Get your answers by asking now.