Example code for Episode 15

This commit is contained in:
Erik C. Thauvin 2014-05-02 03:04:25 -07:00
parent 9e72575265
commit 14fc8c4a6f
5 changed files with 126 additions and 15 deletions

View file

@ -10,10 +10,10 @@
import textwrap
# Print opening title
print "================================ CODING 101 ================================"
print '================================ CODING 101 ================================'
# Open the file
episodeFile = open("coding101.txt", "r")
episodeFile = open('coding101.txt', 'r')
# Read the lines into a list
linesFromFile = episodeFile.readlines()
# Close the file
@ -25,25 +25,25 @@ while run:
# Print an empty line
print
# Print the episode list
print "Choose an episode:"
print 'Choose an episode:'
print
# Loop through the episodes list.
count = 0
for line in linesFromFile:
# Increment the episode number.
# Increment the episode number
count += 1
# Split the line using tab as the delimiter
# The format is: Date<TAB>Episode Title<TAB>Description
episode = line.split("\t")
episode = line.split('\t')
# Print the episode number & title.
# The episode number is converted into a str, and right justified
print "\t{0}. {1}".format(str(count).rjust(2), episode[1])
print '\t{0}. {1}'.format(str(count).rjust(2), episode[1])
print
# Ask for the episode number
choice = raw_input("Enter 0-" + str(count) + " (or ENTER to quit): ")
choice = raw_input('Enter 1-' + str(count) + ' (or ENTER to quit): ')
# Did you press enter?
if not choice:
@ -52,7 +52,7 @@ while run:
# Validate the selected episode number. It must be a number...
elif choice.isdigit():
# Convert the selection to an integer.
# Convert the selection to an integer
selection = int(choice)
# It must also be between 1 and the total number of episodes
@ -60,14 +60,14 @@ while run:
if 1 <= selection <= count:
print
print
print "============================================================================"
# Get and split the line for the episode, the list is zero-based.
print '============================================================================'
# Get and split the line for the episode, the list is zero-based
episode = linesFromFile[selection - 1].split('\t')
# Print the episode, title
print "Episode #" + choice + ": \"" + episode[1] + '" aired on ' + episode[0]
# Print the episode title and aired date
print 'Episode #' + choice + ": \"" + episode[1] + '" aired on ' + episode[0]
print
# Print the description, using textwrap to make it look pretty.
# Print the description, using textwrap to make it look pretty
print textwrap.fill(episode[2], width=76)
print "============================================================================"
print '============================================================================'
print
raw_input("Press ENTER to continue...")
raw_input('Press ENTER to continue...')