CS111 Introduction to Computer Science Friday, 5 October 2018 0. Office hours - Come see me! 1. Prepping for Wednesday's exam - Types of questions * Short answer (facts) * Predict the output of given code * Write very small amounts of code * Short answer (why?, explain) * [maybe] other - Fair game: anything in the assigned readings, the homework assignments, class, labs, or sample programs - Most likely: topics that have appeared in class, in homework, in labs, in sample programs - Focus: things you find confusing. Ask for help (me, Eva, lab assistants, classmates, #questions) 2. Questions? Here's the code we experimented with in response to a student question about how to make the table look nice in the fancy version of the weather program. You can search the internet for "python formatted strings" to get more info about the {0}, {1} thing. year = 1985 max_temp = 99.0 min_temp = -15.6 avg_high = 1003.6/21.0 print('{0:<8}{1:>7}{2:>7}{3:^12}'.format('Year', 'Max', 'Min', 'Avg High')) print('{0:<8}{1:>7.1f}{2:>7.1f}{3:^12.1f}'.format(year, max_temp, min_temp, avg_high)) 3. Debugging - Error messages * Take any program (e.g. weather.py) * Come up with ways to break its syntax What error messages do you get? * Come up with ways to keep the syntax ok but make it crash anyway What error messages do you get? * Get in the habit of writing down new error messages and what they mean * Practice reading the error messages and extracting all their meaning - Compile-time errors (is this code syntactically incorrect, so it crashes before executing any code?) - Run-time errors (does the code fail while running?) - Here are some of the errors people in class found while experimenting in class IndentationError: expected an indented block TypeError: '>' not supported between instances of 'str' and 'float' FileNotFoundError: [Errno 2] No such file or directory: 'weather.csv' SyntaxError: invalid syntax ValueError: too many values to unpack (expected 2) ... In summary: - You should practice reading your error messages. - Error messages are sometimes mysterious, but they often give you essential information about how to fix the problem, and they always give you a line number where you can start looking for the trouble. - You can practice at reading errors by doing a wacky exercise: take an already-working program and break iton purpose to see what error you get. Restore the program to its working state and repeat. 4. Questions from last week - Where do parameters come from? Here are some possibilities. (1) Hard-code it radius = 4.2 volume = volume_of_sphere(radius) (2) Ask the user radius = float(input('What is the radius of your sphere? ')) volume = volume_of_sphere(radius) (3) Command-line arguments python3 spherestats.py volume 4.2 import sys radius = sys.argv[2] volume = volume_of_sphere(radius) (4) Read it from a file Suppose a file called spheres.txt has lines like 10 15 -5 4.2 which says "there's a sphere with radius 4.2 centered at (10, 15, -5). f = open('spheres.txt') for line in f: line = line.strip() fields = line.split() radius = float(fields[3]) volume = volume_of_sphere(radius) (5) Compute it from something else ...spaceship is at location (x, y, z)... ...spherical shockwave weapon centered at spaceship expands at 1.4 units per second... ...it has been three seconds since the weapon went off... radius = seconds_since_firing * expansion_rate volume = volume_of_sphere(radius) - Reminder on searching for help (1) "python how to tell if character is a letter" (i.e. what's the name of the thing I need?) --> (StackOverflow?) ...isalpha()... (2) "python isalpha" --> (docs.python.org) official documentation (3) "python isalpha examples" --> code snippets, suggestions, etc.