Explain things.

This commit is contained in:
Josh Mudge 2019-03-11 20:02:10 -06:00
parent b4bb1c228e
commit 0513ea2283
1 changed files with 21 additions and 18 deletions

View File

@ -1,33 +1,36 @@
# Import the argv tool from system tools.
from sys import argv from sys import argv
# Grab some command line arguments with argv.
script, input_file = argv script, input_file = argv
def print_all(f):
print(f.read())
def rewind(f): def print_all(f): # Define a function with one argument.
f.seek(0) print(f.read()) # Read an a file passed to this function.
def print_a_line(line_count, f): def rewind(f): # Define a function with one argument.
print(line_count, f.readline()) f.seek(0) # Start from the first line.
current_file = open(input_file) def print_a_line(line_count, f): # Define a function with two arguments.
print(line_count, f.readline()) # Print the line.
print("First let's print the whole file:\n") current_file = open(input_file) # Open the file input_file as "current_file"
print_all(current_file) print("First let's print the whole file:\n") # Tell the user we're printing the whole file.
print("Now let's rewind, kind of like a tape.") print_all(current_file) # Print the whole file.
rewind(current_file) print("Now let's rewind, kind of like a tape.") # Tell the user we're starting over.
print("Let's print three lines:") rewind(current_file) # Print the first line.
current_line = 1 print("Let's print three lines:") # Tell the user we're printing three lines.
print_a_line(current_line, current_file)
current_line = current_line + 1 current_line = 1 # Set the current line to the second line.
print_a_line(current_line, current_file) print_a_line(current_line, current_file) # Print the current line.
current_line = current_line + 1 current_line = current_line + 1 # Increment the current line by one, making it the third line.
print_a_line(current_line, current_file) print_a_line(current_line, current_file) # Print the current line.
current_line = current_line + 1 # Increment the current line by one, making it the fourth line.
print_a_line(current_line, current_file) # Print the current line.