|
|
|
Announcements
|
|
|
|
|
Welcome party
|
|
|
|
|
HW1 due, HW2 and partners posted
|
|
|
|
|
Questions
|
|
|
|
|
Practice: function to convert miles to kilometers
|
|
|
|
|
1.61 km per mile
|
|
|
|
|
Problem: what icon to display in weather app?
|
|
|
|
|
Simplified problem: difference between current and “hot” temperature (i.e., should the app show the current temperature in red)
|
|
|
|
|
Decision time
|
|
|
|
|
we can do this with Python’s if
|
|
|
|
|
anatomy of if
|
|
|
|
|
if <condition>: <indented lines only executed when condition is True>
|
|
|
|
|
need to make program behavior conditional on state of the world
|
|
|
|
|
diagram possible states and actions
|
|
|
|
|
let’s say text has more than two settings (yellow, orange, red)
|
|
|
|
|
if/elif/else nice way to structure this decision
|
|
|
|
|
Practice: number guessing game
|
|
|
|
|
given a player’s guess and a secret number, print “cold” “warm” or “lava”
|
|
|
|
|
only print one hint
|
|
|
|
|
boolean expressions
|
|
|
|
|
relational operators
|
|
|
|
|
less than (<), less than or equal to (<=)
|
|
|
|
|
greater than (>), greater than or equal to (>=)
|
|
|
|
|
equal (==)
|
|
|
|
|
not equal (!=)
|
|
|
|
|
can use and, or to combine expressions
|
|
|
|
|
check if variable x is positive number at most 10: x > 0 and x <= 10
|
|
|
|
|
can use parentheses to control evaluation
|
|
|
|
|
current_temp > hot_temp and chance_of_rain > 0.4 or chance_of_clouds > 0.5
|
|
|
|
|
(current_temp > hot_temp and chance_of_rain > 0.4) or chance_of_clouds > 0.5
|
|
|
|
|
same as first version, by default ands and ors are evaluated left to right
|
|
|
|
|
current_temp > hot_temp and (chance_of_rain > 0.4 or chance_of_clouds > 0.5)
|
|
|
|
|
Practice: write an absolute value function
|
|
|
|
|
Let’s imagine our guessing game is played over many rounds
|
|
|
|
|
We might copy and paste the code for each round with minor changes
|
|
|
|
|
Is this a place where a function would be useful?
|
|
|
|
|
What part of the code should be moved into a function and what would it look like?
|
|
|
|
|
Partner activity
|
|
|
|
|
Here is a common interview question in CS.
|
|
|
|
|
Write a program called fizzbuzz.py that asks the user to input a number num and outputs the following.
|
|
|
|
|
Outputs “fizz” if num is divisible by 3, Outputs “buzz” if num is divisible by 5, Outputs “fizzbuzz” if num is divisible by both 3 and 5, and Outputs num otherwise.
|
|
|
|
|
Modify your program so that it also outputs “jazz” if num is divisible by 7. Note that it should also output “fizzjazz” if it is divisible by 3 and 7, and so on for all other combinations above
|
|
|