1 of 134

Lesson 5

Random Numbers and Comparisons

csinschools.com

2 of 134

Previously, on CS in Schools...

  • What is one disadvantage of using goto loops instead of while loops?

2

3 of 134

Previously, on CS in Schools...

  • What is the different about these two programs when they run?

3

keep_going = True

​

if keep_going == True:

​

print("Hello!")

keep_going = True

​

while keep_going == True:

​

print("Hello!")

4 of 134

Previously, on CS in Schools...

  • How many times will Boo! be shown on the screen?

4

count = 0

keep_going = True

​

while keep_going == True:

count = count + 1

print("Boo!")

if count == 3:

keep_going = False

​

print("All done!")

​

This code is trying to be tricky..! Look at the code carefully.

5 of 134

Learning Objectives

By the end of this lesson, you should be able to:

  • understand the syntax of various comparison operators
  • use comparison operators to check for numbers which are less than or greater than other numbers
  • know how to use the random module to generate random numbers

5

6 of 134

Python Reference Sheet

6

Python Reference Sheet

  • Print it out and use it!
  • Get it online. (Microsoft)

​

7 of 134

Demo: Guess My Age!

7

8 of 134

8

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

Let's see how we check if a number is higher or lower than another number...

9 of 134

9

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

10 of 134

10

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

11 of 134

11

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

12 of 134

12

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

13 of 134

13

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

14 of 134

14

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

15 of 134

15

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

True!

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

16 of 134

16

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

17 of 134

17

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

18 of 134

18

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

19 of 134

19

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

20 of 134

20

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

Let's say the user types in "18"

21 of 134

21

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

22 of 134

22

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

23 of 134

23

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

"18"

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

24 of 134

24

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

"18"

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

25 of 134

25

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

"18"

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

26 of 134

26

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

"18"

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

27 of 134

27

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

28 of 134

28

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

29 of 134

29

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

30 of 134

30

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

31 of 134

31

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

32 of 134

32

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

The > sign is a comparison operator

33 of 134

33

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

Comparison operators are used to compare the left and right hand sides.

34 of 134

34

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

The > sign means check if the left hand side is greater than the right hand side

35 of 134

35

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

… so is my_age greater than your_guess?

36 of 134

36

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

13

18

>

… so is my_age greater than your_guess?

37 of 134

37

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

13

18

>

… so is �13 > 18?

>

38 of 134

38

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

13

18

>

False!

… so is �13 > 18?

False!

>

39 of 134

39

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

False!

40 of 134

40

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

41 of 134

41

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

42 of 134

42

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

43 of 134

43

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

44 of 134

44

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

The < sign is another comparison operator

45 of 134

45

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

The < sign means check if the left hand side is less than the right hand side

46 of 134

46

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

… so is my_age less than your_guess?

47 of 134

47

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

13

<

18

… so is my_age less than your_guess?

48 of 134

48

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

13

<

18

… so is �13 < 18?

<

49 of 134

49

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

13

<

18

True!

… so is �13 < 18?

True!

<

50 of 134

50

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

True!

51 of 134

51

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

52 of 134

52

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

53 of 134

53

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

54 of 134

54

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

55 of 134

55

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

True!

56 of 134

56

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

57 of 134

57

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

58 of 134

58

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

59 of 134

59

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

60 of 134

60

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

Let's say the user types "13"

61 of 134

61

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

62 of 134

62

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

18

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

63 of 134

63

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

"13"

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

64 of 134

64

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

"13"

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

65 of 134

65

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

"13"

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

66 of 134

66

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

"13"

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

67 of 134

67

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

"13"

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

68 of 134

68

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

69 of 134

69

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

70 of 134

70

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

71 of 134

71

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

72 of 134

72

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

73 of 134

73

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

… so is �13 > 13?

74 of 134

74

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

False!

False!

… so is �13 > 13?

75 of 134

75

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

False!

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

76 of 134

76

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

77 of 134

77

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

78 of 134

78

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

79 of 134

79

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

80 of 134

80

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

… so is �13 < 13?

81 of 134

81

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

False!

False!

… so is �13 < 13?

82 of 134

82

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

83 of 134

83

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

84 of 134

84

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

If my_age is NOT greater than and it's NOT less than...

85 of 134

85

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

Then it MUST be equal to your_guess!

86 of 134

86

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

87 of 134

87

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

88 of 134

88

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

89 of 134

89

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

90 of 134

90

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

91 of 134

91

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

False!

92 of 134

92

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

93 of 134

93

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

94 of 134

94

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

your_guess

13

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

95 of 134

95

guessing = True

​

my_age = 13

​

while guessing == True:

print("How old?")

your_guess = input("Guess: ")

​

your_guess = int(your_guess)

​

if my_age > your_guess:

print("I am older")

elif my_age < your_guess:

print("I am younger")

else:

print("You got it!")

guessing = False

​

print("Bye now!")

​

Start

Set guessing to True

Set my_age to 13

Repeat IF guessing is True

Display "How old?"

Assign to your_guess variable

Convert to an int

Is my_age greater than your_guess?

Display "I am older"

Display "You got it!"

Display "Bye now!"

End

True

True

False

False

Let user input their guess

Is my_age less than your_guess?

False

Display "I am younger"

True

Set guessing to False

96 of 134

Comparison Operators

The table below summarises some comparison operators we will use:

96

Symbol

Description

Example(s) of True cases:

==

Equal to

if 13 == 13:

<

Less than

if 12 < 13:

>

Greater than

if 14 > 13:

<=

Less than OR equal to

if 10 <= 13:

if 13 <= 13:

>=

Greater than OR equal to

if 14 >= 13:

if 13 >= 13:

!=

Not equal to

if 12 != 13:

97 of 134

Activity: Guess My Height!

97

Activity 05.01�Guess My Height

​

Helpful Hints

  • Worksheet - complete this first!!�
  • Check the cheat sheet for the colours you can use!

98 of 134

Demo: Gimme Five!

98

99 of 134

Random numbers

  • Quickly... think of a number between 1 and 100
  • How did you come up with this number?

99

100 of 134

Random numbers

  • Quickly... think of a number between 1 and 100
  • How did you come up with this number?
  • How do you think a computer comes up with "random numbers"?

100

101 of 134

Random numbers

  • Quickly... think of a number between 1 and 100
  • How did you come up with this number?
  • How do you think a computer comes up with "random numbers"?
    • Hint: It's not really random
    • It's a complicated process that generates numbers that appear to be (but are not really) random
    • It's too complicated to describe here, and way too complicated to code ourselves!

101

102 of 134

Functions to the rescue!

  • There is a function called randint that we can use to let the computer produce a 'random' integer for us
  • It is found in a module called random

102

For more information on modules and using other people's code, take a look here.

103 of 134

Functions to the rescue!

  • There is a function called randint that we can use to let the computer produce a 'random' integer for us
  • It is found in a module called random

103

from random import *

rand_num = randint()

104 of 134

Functions to the rescue!

  • There is a function called randint that we can use to let the computer produce a 'random' integer for us
  • It is found in a module called random

104

from random import *

rand_num = randint()

105 of 134

Functions to the rescue!

  • There is a function called randint that we can use to let the computer produce a 'random' integer for us
  • It is found in a module called random

105

from random import *

rand_num = randint()

106 of 134

Functions to the rescue!

  • However, randint is a function that requires two arguments
    • The lower and upper limit of the random number

106

from random import *

rand_num = randint()

107 of 134

Functions to the rescue!

  • However, randint is a function that requires two arguments
    • The lower and upper limit of the random number
  • For example, if you wanted a random number between 10 and 20, you would do this

107

from random import *

rand_num = randint()

108 of 134

Functions to the rescue!

  • However, randint is a function that requires two arguments
    • The lower and upper limit of the random number
  • For example, if you wanted a random number between 10 and 20, you would do this

108

from random import *

rand_num = randint()

109 of 134

Functions to the rescue!

  • However, randint is a function that requires two arguments
    • The lower and upper limit of the random number
  • For example, if you wanted a random number between 10 and 20, you would do this

109

from random import *

rand_num = randint(10)

110 of 134

Functions to the rescue!

  • However, randint is a function that requires two arguments
    • The lower and upper limit of the random number
  • For example, if you wanted a random number between 10 and 20, you would do this

110

from random import *

rand_num = randint(10)

111 of 134

Functions to the rescue!

  • However, randint is a function that requires two arguments
    • The lower and upper limit of the random number
  • For example, if you wanted a random number between 10 and 20, you would do this

111

from random import *

rand_num = randint(10, 20)

112 of 134

Functions to the rescue!

  • However, randint is a function that requires two arguments
    • The lower and upper limit of the random number
  • For example, if you wanted a random number between 10 and 20, you would do this

112

from random import *

rand_num = randint(10, 20)

Notice we use a comma (,) to separate the two arguments

113 of 134

Functions to the rescue!

  • This will generate a random number between 10 and 20
    • Including 10 and 20 themselves

113

from random import *

rand_num = randint(10, 20)

114 of 134

Functions to the rescue!

114

from random import *

rand_num = randint(10, 20)

115 of 134

Functions to the rescue!

115

randint(10, 20)

​

from random import *

rand_num = randint(10, 20)

116 of 134

Functions to the rescue!

116

randint(10, 20)

​

12

from random import *

rand_num = randint(10, 20)

117 of 134

Functions to the rescue!

117

randint(10, 20)

​

12

from random import *

rand_num = randint(10, 20)

118 of 134

Functions to the rescue!

118

randint(10, 20)

12

from random import *

rand_num = randint(10, 20)

119 of 134

Functions to the rescue!

119

randint(10, 20)

​

12

from random import *

rand_num = randint(10, 20)

120 of 134

Functions to the rescue!

120

randint(10, 20)

​

12

rand_num

from random import *

rand_num = randint(10, 20)

121 of 134

Functions to the rescue!

121

randint(10, 20)

​

12

rand_num

12

from random import *

rand_num = randint(10, 20)

122 of 134

Functions to the rescue!

122

randint(10, 20)

​

12

rand_num

12

from random import *

rand_num = randint(10, 20)

123 of 134

123

from random import *

​

keep_rolling = True

​

while keep_rolling == True:

print("If you a 5 or more..")

input("Press [enter] to roll")

dice = randint(1, 6)

print("you rolled a " + dice)

​

if dice >= 5:

print("You beat me!")

keep_rolling = False

else:

print("Keep trying!")

​

print("Bye now")

Start

Import random module

Set keep_rolling to True

Repeat if keep_rolling is True

Display "If you roll 5 or more, you win!"

Wait for user to press enter

Generate a random number between 1 and 6

Display the dice variable

Is dice greater than or equal to 5?

Display "You beat me!"

Set keep_rolling to False

Display "Keep Trying!"

Display "Bye now"

End

True

True

False

False

Assign it to the dice variable

Let's take a look at how random numbers were used in our demo.

124 of 134

124

from random import *

​

keep_rolling = True

​

while keep_rolling == True:

print("If you a 5 or more..")

input("Press [enter] to roll")

dice = randint(1, 6)

print("you rolled a " + dice)

​

if dice >= 5:

print("You beat me!")

keep_rolling = False

else:

print("Keep trying!")

​

print("Bye now")

Start

Import random module

Set keep_rolling to True

Repeat if keep_rolling is True

Display "If you roll 5 or more, you win!"

Wait for user to press enter

Generate a random number between 1 and 6

Display the dice variable

Is dice greater than or equal to 5?

Display "You beat me!"

Set keep_rolling to False

Display "Keep Trying!"

Display "Bye now"

End

True

True

False

False

Assign it to the dice variable

125 of 134

125

from random import *

​

keep_rolling = True

​

while keep_rolling == True:

print("If you a 5 or more..")

input("Press [enter] to roll")

dice = randint(1, 6)

print("you rolled a " + dice)

​

if dice >= 5:

print("You beat me!")

keep_rolling = False

else:

print("Keep trying!")

​

print("Bye now")

Start

Import random module

Set keep_rolling to True

Repeat if keep_rolling is True

Display "If you roll 5 or more, you win!"

Wait for user to press enter

Generate a random number between 1 and 6

Display the dice variable

Is dice greater than or equal to 5?

Display "You beat me!"

Set keep_rolling to False

Display "Keep Trying!"

Display "Bye now"

End

True

True

False

False

Assign it to the dice variable

We need to import the random module first.

126 of 134

126

from random import *

​

keep_rolling = True

​

while keep_rolling == True:

print("If you a 5 or more..")

input("Press [enter] to roll")

dice = randint(1, 6)

print("you rolled a " + dice)

​

if dice >= 5:

print("You beat me!")

keep_rolling = False

else:

print("Keep trying!")

​

print("Bye now")

Start

Import random module

Set keep_rolling to True

Repeat if keep_rolling is True

Display "If you roll 5 or more, you win!"

Wait for user to press enter

Generate a random number between 1 and 6

Display the dice variable

Is dice greater than or equal to 5?

Display "You beat me!"

Set keep_rolling to False

Display "Keep Trying!"

Display "Bye now"

End

True

True

False

False

Assign it to the dice variable

127 of 134

127

from random import *

​

keep_rolling = True

​

while keep_rolling == True:

print("If you a 5 or more..")

input("Press [enter] to roll")

dice = randint(1, 6)

print("you rolled a " + dice)

​

if dice >= 5:

print("You beat me!")

keep_rolling = False

else:

print("Keep trying!")

​

print("Bye now")

Start

Import random module

Set keep_rolling to True

Repeat if keep_rolling is True

Display "If you roll 5 or more, you win!"

Wait for user to press enter

Generate a random number between 1 and 6

Display the dice variable

Is dice greater than or equal to 5?

Display "You beat me!"

Set keep_rolling to False

Display "Keep Trying!"

Display "Bye now"

End

True

True

False

False

Assign it to the dice variable

We use the randint() function to generate a random number between 1 and 6

128 of 134

128

from random import *

​

keep_rolling = True

​

while keep_rolling == True:

print("If you a 5 or more..")

input("Press [enter] to roll")

dice = randint(1, 6)

print("you rolled a " + dice)

​

if dice >= 5:

print("You beat me!")

keep_rolling = False

else:

print("Keep trying!")

​

print("Bye now")

Start

Import random module

Set keep_rolling to True

Repeat if keep_rolling is True

Display "If you roll 5 or more, you win!"

Wait for user to press enter

Generate a random number between 1 and 6

Display the dice variable

Is dice greater than or equal to 5?

Display "You beat me!"

Set keep_rolling to False

Display "Keep Trying!"

Display "Bye now"

End

True

True

False

False

Assign it to the dice variable

129 of 134

129

from random import *

​

keep_rolling = True

​

while keep_rolling == True:

print("If you a 5 or more..")

input("Press [enter] to roll")

dice = randint(1, 6)

print("you rolled a " + dice)

​

if dice >= 5:

print("You beat me!")

keep_rolling = False

else:

print("Keep trying!")

​

print("Bye now")

Start

Import random module

Set keep_rolling to True

Repeat if keep_rolling is True

Display "If you roll 5 or more, you win!"

Wait for user to press enter

Generate a random number between 1 and 6

Display the dice variable

Is dice greater than or equal to 5?

Display "You beat me!"

Set keep_rolling to False

Display "Keep Trying!"

Display "Bye now"

End

True

True

False

False

Assign it to the dice variable

We use >= (greater than or equal to) comparison operator to check if they rolled 5 or greater

130 of 134

Activity: Dance Off!

130

Activity 05.02�Dance Off

​

Helpful Hints

  • Look through the slides for examples of random number generation�
  • Check the cheat sheet for useful code

131 of 134

Activity: Guess My Number!

131

Activity 05.03�Guess My Number

​

Helpful Hints

  • Watch the tutorial video.�
  • Check the cheat sheet for useful code

132 of 134

Summary

  • Comparison operators are used to check for numbers which are less than or greater than other numbers
  • The random module is used to generate random numbers

132

133 of 134

Reflection: Exit pass

  • What is one new thing you learnt today?
  • Write it on a sticky note and stick it on the board before you leave the classroom

133

134 of 134

License Information

These CS in Schools lessons plans, worksheets, and other materials were created by Toan Huynh and Hugh Williams. They are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Images, in order of appearance:

​

​

​

​

​

134