From 0513ea2283ae87e0d3ce9c29b0d15c2ae4a77f04 Mon Sep 17 00:00:00 2001 From: Josh Mudge Date: Mon, 11 Mar 2019 20:02:10 -0600 Subject: [PATCH] Explain things. --- 3exercises/ex20.py | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/3exercises/ex20.py b/3exercises/ex20.py index 2dd61b9..8a0ba02 100644 --- a/3exercises/ex20.py +++ b/3exercises/ex20.py @@ -1,33 +1,36 @@ +# Import the argv tool from system tools. from sys import argv +# Grab some command line arguments with argv. script, input_file = argv -def print_all(f): - print(f.read()) -def rewind(f): - f.seek(0) +def print_all(f): # Define a function with one argument. + print(f.read()) # Read an a file passed to this function. -def print_a_line(line_count, f): - print(line_count, f.readline()) +def rewind(f): # Define a function with one argument. + 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_a_line(current_line, current_file) +print("Let's print three lines:") # Tell the user we're printing three lines. -current_line = current_line + 1 -print_a_line(current_line, current_file) +current_line = 1 # Set the current line to the second line. +print_a_line(current_line, current_file) # Print the current line. -current_line = current_line + 1 -print_a_line(current_line, current_file) +current_line = current_line + 1 # Increment the current line by one, making it the third line. +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.