Intro to
Programming
(with Python)

Fall 2019
course
site

Thu Oct 10

First up: discuss the homework due today.

My sample answers are in code/homework/oct10 :

Please take these examples of mine as "good coding practice" and where you can follow this format for everything else you do this semester: docstrings, doctests (where appropriate, they aren't always), and short, clearly defined functions with clean arguments (inputs) and return value (outputs).

Any questions about anything?

upcoming schedule

Here's the schedule I have in mind the rest of the term, with assignments due on the days marked with a *.

Oct        10
     15    17*     conditionals & midterm project proposal
           24
     29*   31      chap 8 - booleans
Nov   5*    7      midterm project due (also read chap 9, simulation & design)
     12*   14      chap 10 - defining classes
     19*   21      chap 11 - collections & final project proposal
     26*           chap 12 - object design
Dec   3*    5      chap 13 - algorithms
     10*      13*  final project class presentations; write-up due Fri 13th

The midterm is due Tuesday Nov 4. We'll have in-class presentations of your final project on the last day of class, Dec 10. And the final project writeup is due Fri Dec 13, with extensions until Mon Dec 16 available upon request.

I will post the assignment due next Thursday within the next day or so - start by please reading chapter 7 this week.

Decision structures

I'll either just show examples or use the textbook's slides to walk through the topics in chapter 7 .

"max of 3 numbers" example

Which is biggest?

First try: "if x1 >= x2 >= x3" ... Hmmm. Valid in python but not in most code. Also, (5,2,4) gives False; doesn't identify x1 as biggest.

Second try : "if x1 >= x2 and x1 >= x3" ... better i. More about "and" in the next chapter.

if x1 >= x2 and x1 >= x3: 
    maxval = x1
elif x2 >= x1 and x2 >= x3: 
    maxval = x2
else:
    maxval = x3

or how about this instead

if x1 >= x2:
    if x1 >= x3:
        maxvval = x1 
    else:
        maxval = x3
else:
    if x2 >= x3:
        maxval = x2
    else:
        maxval = x3

Draw that as a flowchart and trace through some examples.

Or we could just test each in turn ...

maxval = x1
if x2 > maxval:
    maxval = x2
if x3 > maxval:
    maxval = x3

This approach scales nicely to bigger collections.

In this case python has a built-in ... max(x1, x2, x3). :)


The programs that we worked on in class are attached.

https://cs.marlboro.college /cours /fall2019 /python /notes /chap7
last modified Sun December 22 2024 9:06 am

attachments [paper clip]

  last modified size
TXT errors.py Sun Dec 22 2024 09:06 am 175B
TXT userinput.py Sun Dec 22 2024 09:06 am 368B