Explore Python 1
Conditionals
Compared with Scratch
Understanding conditionals correctly is very important...
A programmer's wife tells him: "Run to the store and pick up a loaf of bread. And if they have eggs, get a dozen".
The programmer comes home with 12 loaves of bread.
What does the programmer get from the store?
Conditionals 1
Scratch
Python
Defensive programming:
Conditionals 2
If condition1 :
do things
elif condition2 :
do others
else:
do this
Zoom Poll: Which one is better? Why?
Program 1 Program 2
Python
If condition1 :
do things
elif condition2 :
do others
elif condition3 :
do this
What’s the difference?
Defensive programming:
2. Do something with unexpected input
If condition1:
do things
else:
do other things
Nested Conditionals 2.1
The elif Clause (fun fact :)
You may have noticed that the keyword is spelled ‘elif’, without ‘se’. If you write it as “elseif”, with ‘se’, Python will tell you that it is not the correct spelling.
Why not? Because Guido van Rossum (the inventor of Python) said so.
In fact, Guido resisted any suggestion that it even be permitted as a valid alternative spelling. He said:
“If you want to spell it with ‘se’, you may do it very simply:
Step 1—Make up your own language.
Step 2—Make it popular.”
Case AB:
Case CDE:
Case FGH:
Nested Conditionals 2.5
Exercise 1 ---->>>
What will be printed if you
un/comment the various values of
a_variable and b_variable?
Predict what
will be printed, and write your predictions as comments in the Trinket (under the heading “Exercise 1”).
You may want to run the code to verify your predictions.
Case AB:
Case CDE:
Case FGH:
Zoom Poll
New Trinket:
Explore Python 1
Exercise: Conditionals 3
Remember:
The common temperature scales are Celsius (C), Fahrenheit (F), Kelvin (K).
(Did you hear about the man who was cooled to absolute zero? He's 0K now :)
Python
Exercise 2:
Temperature Conversion
Do defensive programming:
Conditionals 4
Hint: use mod (the % operator). Run the following and figure out how to use mod based on the result of the code below (try different numbers of diners, like 3, 3.7, 5, 5.1, to see what the results are):
diners = float(input("how many diners?"))
print diners % 1
Hint: use the round() built-in Python function, to print just 2 decimal places, e.g., 23.45
Scratch
Python
else:
you do the hard math … :)
Exercise 3 - fix & enhance with defensive programming
Conditionals 5
Exercise 4:
Extra Experience: add “raising to the power” (exponentiation) operation:
look up the math operation for that
Conditionals with Logic
You can use more sophisticated conditions with the ‘if’ statements.
E.g. (on an assembly line or a factory):
weight = float(input(“what’s the part weight?”) # lbs.
height = int(input(“what’s the part height?”) # inches
color = input(“what’s the part color?”) # color name
if weight > 15 and height < 50 and color == ‘purple’:
print ‘This part does not fit the bill’
else:
print ‘This is the correct part!’
You can use logic operations like: and or not
You can use math comparisons like: == != <= >=
Exercise 5
You are driving a little too fast, and a police officer stops you. Write code to compute the result (getting a ticket :(, by printing “no ticket”, “small ticket”, or “big ticket”, depending on the speed (see below):
If speed is 65 or less, the result is “no ticket”. If speed is between 66 and 80 inclusive, the result is “small ticket”. If speed is 81 or more, the result is “big ticket”. Unless it is your birthday, in which case, the policeman will be stricter and your speed needs to be 5 lower in all cases (e.g., you need to drive between 61 and 75 to get “small ticket”, and so on).
Extra Fun (and Experience) with conditionals
Do all the sections in Ex. Ex. 3 - More conditionals in the doc
Explore Python 2 (Trinket) - Ex. Ex. 3
When done with conditionals, create a new Trinket, and try to figure out how to draw shapes by starting to explore
Python Turtle Graphics. (or on GD)
Extra Fun (and Exploring) with Turtles
When done with conditionals, create a new Trinket, and try to figure out how to draw shapes by starting to explore
Python Turtle Graphics. (or on GD)
Backup slides
This is the end of the presentations.
Proceed at your own risk :)
This is (almost) a blank slide :)
Partial Solution: Conditionals 2
Remember:
Scratch
Python
Do defensive programming:
Scratch
FunFact: Algorithm for supporting equality-check ==
http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3
11.9.3 The pseudocode for the Equality Comparison Algorithm in JavaScript
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:
(continued on the right …)