Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

What would be a good set of programming exercises that would help Python newbies to learn the use of the "if ... else" construct? I could cook up the following, do you know of any more?

  1. Find the largest/smallest of three numbers.
  2. Given a date (year, month, day), find the next date.

Most of the intended audience have not had much of an exposure to programming before, and I am keen on getting them used to thinking correctly about "if ... else" (and all the rest of it, in due course).

like image 653
gphilip Avatar asked Aug 03 '10 08:08

gphilip


4 Answers

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)
like image 117
djna Avatar answered Nov 08 '22 19:11

djna


"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.

like image 43
paxdiablo Avatar answered Nov 08 '22 21:11

paxdiablo


Try a simple game, like if you press 'L', turn left, if you press 'R', turn right, if there's a monster, you die etc.

like image 1
Skilldrick Avatar answered Nov 08 '22 20:11

Skilldrick


There are numerous of options here. Maybe let them build a simple calculator, taking into account division by zero, odd/even numbers and the like.

Edit: Found this simple excercise on if-else (in java) which can be transformed into Python.

like image 1
Benny Skogberg Avatar answered Nov 08 '22 19:11

Benny Skogberg