Python Advanced #12 - Searching for a value in a text file

This post builds off of the coding that was developed in Python Advanced #10 and #11.  In this post, we want to ask the user to input a value that they are looking for in a text file.  In the code, we want to track the line where the value appears in the text file (line_count) and track the number of times the value appears in the file (vals_count).

Below is an image of the text file that we will be searching.




The coding to search the text file and to track the number of occurrences and the line(s) where this value appears is shown below.



Several simulations of this program are shown below.

If we search for "500", we expect that the program will indicate that the value is not found.



If we search for "43", we expect the program will indicate that the value is found once and is found in line 2.



If we search for "517", we expect the program will indicate that the value is found twice and is found in lines 1 and 4.



Comments