COS 100: Introduction to Programming
Fall 2019
Lab 14: Bonus lab
Due to time constraint in this course, we've skipped over a lot of things. This optional bonus lab contains a few ideas that are very useful.Lab 14A: Control flow in loops
- In the body of a
for
or awhile
loop, you can usecontinue
to skip the rest of the loop and start the next iteration or usebreak
to get out of the loop. -
Run this code to understand the effect of
break
andcontinue
:i = 0 while True: if i == 10: break i = i + 1 print(i) if i % 2 != 0: continue print("even:", i)
- Suppose
gradebook.txt
has the following format:# max score Max Perfect,6,10,20,20 # what follows are students Rory Gilmore,5,9,13,7 Paris Geller,6,10,20,19 -- Paris, as always, did well. Rory drifted off at the end.
where a line starting with#
is a comment to be ignored and a line that is just--
marks the end of student records (what's below should be ignored). -
Modify your
load_file
from the gradebook homework project to ignore comments (lines starting with#
) and stop processing when (if) a line--
is reached. - See Zelle 8.5 for more details.
Lab 14B: Exceptions
- When an error occurs, the program raises an exception.
- A
try
block allows you to catch an exception (of a certain type) and do something in theexcept
block.try: num = int(input("Integer please: ")) except ValueError: print("That was not an integer!")
- Modify your silo homework so that if the user provided bad input, you will repeatedly prompt the user until a good input is given.
- Modify the animation from the graphics lab to exit gracefully (close the window, say bye to the user, and don't display error messages) when the user types Ctrl-C to quit.
- See Zelle 7.4 for more details.
Lab 14C: String formatting
- Strings have a
format()
method. -
Run the code below and deduce its usage:
name = input('Name, please: ') number = int(input('Number, please: ')) print('Hi {0}. You like {1}?'.format(name, number))
- What's the curly braces
{
and}
for? What happens if you swap the0
and the1
? What if you omit the0
and the1
? -
You can make pretty columns with some special features of formatted strings.
people = ['Alice', 'Bob', 'Chrysanthemum', 'Donald'] hourlyWages = [15.456, 6.8, 254.25, 20] for k in range(len(people)): print('{0:15}{1:f}'.format(people[k], hourlyWages[k]))
- Look at the official documentation to see if you can figure out a way to force the hourly wages to be right-justified with exactly two digits beyond the decimal point.
- See Zelle 5.8.2 for more details.
Lab 14D: Dictionaries
- A dictionary can store key-value pairs.
wages = {'Alice': 15.456, 'Bob': 6.8, 'Donald': 20} wages['Chrysanthemum'] = 254.25 print(wages) print(wages['Alice']) for name in wages: print(name, wages[name]) del wages['Bob'] print('Alice' in wages) print('Bob' in wages) print(20 in wages) for (name, wage) in wages.items(): print(name, wage)
- See Zelle 11.7 for more details.
Done with all that? Remember that you can play with Kattis. You can also ask me for an extra secret bonus lab.