PROBLEM: Read a string from standard input using python. Then, iterate over a text file called "cards.txt" and, for each line of the file that starts with the string read from standard input, print that line.
File Iteration: Introduction
A text file is essentially a sequence of lines. Here is a typical coding pattern for processing each line of a file in turn:
- Open the file for reading.
- Use a for loop to iterate over the lines of text, executing a block of one or more statements to process each line.
- Close the file.
- Finish processing (print, return, etc.) if needed
NOTE: You will need to use this pattern, or some variation of it, to solve the remaining coding problems in this assignment.
As the following for loop iterates through each line of the file, the loop variable (`line`) represents the current line of the file as a string of characters.
myFile = open("someTextFile.txt", "r") for line in myFile: do something on each iteration do a second thing on each iteration etc... myFile.close()
PROBLEM: Read a string from standard input using python. Then, iterate over a text file called "cards.txt" and, for each line of the file that starts with the string read from standard input, print that line.
TO DO:
- Add the missing bits of code to complete the partial solution that is provided (replace the _________ s).
- Follow the coding pattern described above.
NOTE: If there are no lines in the file beginning with the given string, there should be no output.

Step by step
Solved in 6 steps with 3 images









