Browse Source

Explain things.

master
Josh Mudge 5 years ago
parent
commit
0513ea2283
  1. 39
      3exercises/ex20.py

39
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.

Loading…
Cancel
Save