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?
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).
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)
"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):
if
statements if you don't want a loop).For nothing but if/else
statements, the leap year one is good. You could also consider:
That's just a smattering of possibilities that you could get away with.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With