# 4 - IF ELSE Statements

1. Run the program and play it. On what line can you spot the 'If statement'?
```import turtle

tina = turtle.Turtle()

Guess = int(input("What is 2 X 7?"))

if Guess == 2*7:
tina.write(str(Guess) + ' is correct!')
tina.penup()
tina.backward(10)
else:
tina.write('You said ' + str(Guess) + '. I got ' + str(2*7))
tina.penup()
tina.backward(10)```

Line 5

Line 7

Line 1

There is no IF statement

2. The following code has a very subtle mistake in it that will stop it from running. Can you spot what it is?
```import turtle

tina = turtle.Turtle()

Guess = int(input("What is 2 X 7?"))

if Guess = 2*7:
tina.write(str(Guess) + ' is correct!')
tina.penup()
tina.backward(10)
else:
tina.write('You said ' + str(Guess) + '. I got ' + str(2*7))
tina.penup()
tina.backward(10)```

A double equal sign (==) on Line 7 is needed

There is no mistake

Line 7 should not have a ":" (colon) after it

Line 7 should have a lowercase 'guess' instead of 'Guess'

3. In the following code, what line/s will come into play if you enter '2' as your age?
```import turtle
tina = turtle.Turtle()
tina.shape('turtle')
tina.penup()

try:
age = int(input("How old are you? (Use numbers)"))
if age >= 10 and age <= 15:
tina.write("You're between 10 and 15 years old")
tina.backward(20)
elif age < 10:
tina.write("You're less than 10 years old")
tina.backward(20)
elif age > 15:
tina.write("You're over 15 years old")
tina.backward(20)
except:
tina.backward(100)
tina.write("I don't think I understand that age.  Are you using numbers?")
tina.backward(20)```

If you enter '2' as your age, it will come up with an error.

Lines 1 to 3

Lines 14 to 16

Lines 11 to 13

4. Why has line 17 to 20 been included in the code?
```import turtle
tina = turtle.Turtle()
tina.shape('turtle')
tina.penup()

try:
age = int(input("How old are you? (Use numbers)"))
if age >= 10 and age <= 15:
tina.write("You're between 10 and 15 years old")
tina.backward(20)
elif age < 10:
tina.write("You're less than 10 years old")
tina.backward(20)
elif age > 15:
tina.write("You're over 15 years old")
tina.backward(20)
except:
tina.backward(100)
tina.write("I don't think I understand that age.  Are you using numbers?")
tina.backward(20)```

If the user enters something other than a number (e.g. the letter "A") it will run the code in the except section.

If the user enters an age of 100 it will run the code in the except section

The except section is just there for elegance - it doesn't actually do anything useful

If the user enters the number '1', it will run the code in the except section

5. In the following code, if you enter '202' it should say "Are you sure you are over 200?", but it doesn't work. Why?
```import turtle
tina = turtle.Turtle()
tina.shape('turtle')
tina.penup()

try:
age = int(input("How old are you? (Use numbers)"))
if age >= 10 and age <= 15:
tina.write("You're between 10 and 15 years old")
tina.backward(20)
elif age < 10:
tina.write("You're less than 10 years old")
tina.backward(20)
elif age > 15:
tina.write("You're over 15 years old")
tina.backward(20)
elif age >200:
tina.write("Are you sure you are over 200?")
tina.backward(20)
except:
tina.backward(100)
tina.write("I don't think I understand that age.  Are you using numbers?")
tina.backward(20)```

This cannot be done as it is too complex

Line 14 should say: elif age > 15 and age <200:

Line 6 should say: try: not 200 - all others accepted:

The except statement should also say on line 20: except

6. What would be an appropriate response for line 27 to say?
```import turtle
tina=turtle.Turtle()
tina.shape('turle')
tina.penup()

try:
how_high = int(input("How high should Tina go? (Use numbers between 200 and -200)"))
tina.goto(0, how_high)
height = tina.pos()[1]

if height > 150 and height <= 200:
tina.write("This is very high!")
elif height > 100 and height <= 150:
tina.write("This is high!")
elif height > 0 and height <= 100:
tina.write("This is high but not too high!")
elif height > -100 and height <= 0:
tina.write("This is low but not too low!")
elif height > -150 and height <= -100:
tina.write("This is low!")
elif height >= -200 and height <= -150:
tina.write("This is very low!")
else:
raise
except:
tina.backward(100)
tina.write("Blah Blah Blah")
tina.backward(20)```

tina.write("Hey, that's not a number between 200 and -200!")

tina.write("Excellent - well done")

tina.write("Hello there")

tina.write("Error, please select numbers above 200 only")

7. Point out the if statements in the following code:

There is an if statement in line 6 to 8

There are no IF statements - but for loops have been used

There are two if statements in this code

There are four if statements in this code

8. Can you spot the IF function in the file helper.py?

There are only except statements

Line 1

Line 112 contains one If statement and there are others too

There are no if statements here

9. This is quite a complex bit of code…complete the comment: If the enemy hits a player, a ……….
```# if enemy hits player, a ___________________________
for enemy in enemies:
myscreen.tracer(0)
if enemy.distance(player) < 15:
life.clear()
stamplife(number-1)
myscreen.tracer(0)
player.goto(randint(-195,195),randint(-195,160))
enemy.goto(randint(-195,195),randint(-195,160))
number -= 1```

life gets taken away

error occurs

window is destroyed

10. What do you think this set of if statements is likely to be doing?
```def checkposition(turtlelist, screen):
myscreen.tracer(0)
for turtle in turtlelist:
x = turtle.xcor()
y = turtle.ycor()
if x > 197:
turtle.setx(-197)
elif x < -197:
turtle.setx(197)
if y > 155:
turtle.sety(-197)
elif y < -197:
turtle.sety(155)
myscreen.tracer(1)
return```

A function to check if the turtle is an enemy

A function to check if turtle has gone off the screen

A function to check if the game is over

A function to check if the user is the right age.