Teacher note: Conditionals with cards¶
A misconception that students have about conditionals is that both the statements or blocks under the if
else are executed each time. For instance:
1if x < 2: 2 print("Programming rules!") 3else: 4 print("This is great!")
Some students think that if
x is 1 when the above code is run, the computer will print Programming rules!” and then continue to the
else and print “This is great!”. In fact, only one of the statements is ever printed, never both.
Code.org has a great “unplugged” activity called Conditional with Cards which can help students learn the “conditional” execution of code.
Two teams take turns drawing from a deck of cards. Based on conditions like the card’s suit, color, or value, the teams can gain or lose points, depending on the “program” being run at the time. The game ends when either team reaches a set number of points, or when you run out of cards.
A deck of cards
A set of “programs” that are made up of
if-elsestatements based on characteristics of a card. Here are a couple of examples (warning: not real Python programs!):
1if Card is Red: 2 Award YOUR team 1 point 3else: 4 Award the OTHER team 1 point
1if the Card's value < 5: 2 Deduct 2 points from YOUR team 3else: 4 Deduct 2 points from the OTHER team
Split the class into two teams, say, Team A and Team B. (More fun: have them choose their own team names!)
Shuffle the cards and place the deck in the middle of the room.
Put a “program” up on the board for all to see.
Have the teams take turns drawing cards and “running” the program to see how many points they score each time (keep the score on the board).
At different points in the game, change the program on the board. Do this several times, so students get practice with different types of conditionals.
Wrap up questions
What type of expressions can follow the
if? (Boolean expressions/logical expressions/true or false)
When is the code after the
elseexecuted? (If AND ONLY IF the condition is false)