Conditionals - Exercises
Contents
Conditionals - Exercises#
Exercise#
Write a conditional to check if a number is bigger than 20. Print some message based on if it is, or isn’t bigger than 20. Your code should contain:
A line to assign a value to a number
An if to check if the number is bigger than 20
An indented line to print something appropriate if it is bigger.
An
else(orelif, butelseis preferred here) line.A second indented line to print something appropriate if the number is smaller than 20.
Run your code with some different values of the number (including exactly 20) and see if it does the right thing.
Exercise#
Write a conditional to check if a number is divisable by 3. HINT We showed in the Conditionals section an example to check if a number was even (is divisable by 2), this would be very similar.
Exercise#
Write a conditional to check:
If a number is greater than 0 (print “greater than 0”)
Or if a number is less than 0 (print “less than 0”)
Or if a number is equal to 0 (print “equal to 0”)
Exercise#
By using what we’ve demonstrated above try to write a code to check if a number is divisible by both 2 and 3. If it is divisible by both say “Yay, I divide by 6”, otherwise say “I wish I divided by 6”.