Contents
- file_handle.py
- mystery.py
- twod_exercises.py
- yearborn.py
file_handle.py 1/4
[top][prev][next]
# Demonstrate file handling exception
# Sara Sprenkle
import sys
def main():
infileName = input("What file do you want to read? ")
try:
inFile = open(infileName, "r")
# normally, would be doing some sort of processing of the file here.
inFile.close()
except IOError as exc: # exc is the name of the thrown exception
print("Error reading \"" + infileName + "\".")
# could be a variety of different problems, so print out
# the exception
print(exc)
sys.exit(1)
outfileName = input("What file do you want to write? ")
try:
outFile = open(outfileName, "w")
outFile.close()
except IOError as exc:
print("Error writing \"" + outfileName + "\".")
print(exc)
main()
mystery.py 2/4
[top][prev][next]
# Practice with 2D lists
# by Sara Sprenkle
def main():
matrix = createMatrix()
print("Before:")
print(matrix)
mystery(matrix)
print("After:")
print(matrix)
def mystery(a):
""" "run" this on A, at right """
for row in range( len(a) ):
for col in range( len(a[0]) ):
if row == col:
a[row][col] = 42
else:
a[row][col] += 1
def createMatrix():
a0 = list(range(1,5))
a1 = list(range(5,9))
a2 = list(range(9,13))
a = []
a.append(a0)
a.append(a1)
a.append(a2)
return a
main()
twod_exercises.py 3/4
[top][prev][next]
# Practice with 2D Lists
# Sara Sprenkle
def main():
rows = int(input("How many rows? "))
columns = int(input("How many columns? "))
print()
print("Correct Matrix Creation:")
print('-'*30)
matrix = create2DList(rows, columns)
print(matrix)
print("\nAssigning matrix[1][2]=3")
matrix[1][2] = 3
print("Result: ")
print(matrix)
print()
print("*"*55)
print("Incorrect Matrix Creation:")
print('-'*30)
matrix = noCreate2DList(rows, columns)
print(matrix)
print("\nAssigning matrix[1][2]=3")
print("Result: ")
matrix[1][2] = 3
print(matrix)
def create2DList(rows, cols):
"""Returns a two-dimensional list filled with 0s that is 'rows' tall and
'cols' wide."""
twodlist = []
for row in range(rows):
row = []
for col in range(cols):
row.append(0)
twodlist.append(row)
return twodlist
def noCreate2DList(rows, cols):
"""Does not create a 2D list because each 'row' points to the same point in
memory."""
twodlist = []
onerow = []
for col in range(cols):
onerow.append(0)
for row in range(rows):
twodlist.append(onerow)
return twodlist
main()
yearborn.py 4/4
[top][prev][next]
# Demonstrate validating user input
# Modified from a student's code from lab assignment
import sys
def main():
#Program mission statement
print("This program determines your birth year")
print("given your age and the current year \n")
try:
age = int(input("Enter your age: "))
# note difference if use eval instead of int in above
currentyear = int(input("Enter the current year: "))
except:
print("ERROR: Your input was not in the correct form.")
print("Enter integers for your age and the current year")
sys.exit()
if age < 0 or age > 115:
print("Come on: you have to be a reasonable age.")
elif currentyear < 0:
print("You need to have a positive year.")
else:
#Subtract age from current year
birthyear=currentyear - age
#Display output to the user
print("You were either born in", birthyear, "or", birthyear-1)
main()
Generated by GNU Enscript 1.6.6.