Exercises on conditional statements¶
For background, please read the conditional statements introduction.
This if statement is not properly defined, and will give a SyntaxError
. Fix it, then run to confirm it prints 'x greater than 4'
:
x = 10
x > 4:
print('x greater than 4')
File "<ipython-input-1-d4c543ec60cc>", line 2
x > 4:
^
SyntaxError: invalid syntax
This if statement also gives a SyntaxError
. Fix and run. It should
print -8
;
p = -2
if p < 0
p = p * 4
print(p)
File "<ipython-input-2-be1f00523226>", line 2
if p < 0
^
SyntaxError: invalid syntax
Another SyntaxError
; fix and run. It should print a divided by 6 is 4
a = 24
if a / 6 == 4:
print('a divided by 6 is 4')
File "<ipython-input-3-60e8e4db26cc>", line 3
print('a divided by 6 is 4')
^
IndentationError: expected an indented block
We want an algorithm to choose a newspaper for someone to read in the dentist’s waiting room, given what we know about their views on Brexit.
Write a cell that prints Times
if voter
has the value Soft Brexit
,
otherwise prints Telegraph
if voter
has the value Hard Brexit
otherwise
prints Guardian
if voter
has value remain
, otherwise prints No idea
.
# Try for different values of voter
voter = 'Hard Brexit'
# Your code here
if voter ...
File "<ipython-input-4-282ab6852901>", line 4
if voter ...
^
SyntaxError: invalid syntax