What are good programming questions to exercise the use of "if ... else" in Python?

"Figure out whether a given year is a leap year" springs to mind almost immediately. Just give 'em the rules and turn 'em loose.

Other possibilities (albeit with stuff other than if statements):

  • Hunt the Wumpus (you may have to google for this one, I'm showing my age).
  • The perennial "detect a win in a Tic Tac Toe (Noughts and Crosses) game" (you could do this with eight if statements if you don't want a loop).
  • Guessing a number between 1 and 100 as quickly as possible (higher, lower).

For nothing but if/else statements, the leap year one is good. You could also consider:

  • Test if a number is a multiple of 3, 5 or 7.
  • Given an age, figure out whether someone's a baby, toddler, child, teenager, adult or old codger.
  • Calculate grades A-F based on final percentage score.
  • Given a number on the roulette table, figure out whether it's red/black, high/low and odd/even.
  • Given a blackjack hand, check if it's okay or bust (this is good since J/Q/K morph into 10). You could also figure out whether to draw another card (if total under 17 for example).

That's just a smattering of possibilities that you could get away with.


It's hard for those of us who have been programming for years to "get" what it feels like to start from nothing. I would steer clear of anything needing more than 5 lines of code.

You need to decide the order in which you cover things such as User Input, Output, if, else, while, for, file io etc.

When covering IF do they already know how to get some user input? Print some output? Code a FOR loop? Do arithmentic on integers? Determine whether a number is divisible by another number?

The answers to these questions constrains your examples for IF.

I'd suggest doing Output, Arithmentic, FOR, Divisibility (modulus), User Input before doing IF.

Then I can pose problems such as

Print the first 100 odd numbers
Determine the factors of a number entered  by the user
Play a number guessing game (User enters a guess, you print YES or Higher or Lower)