Lesson 5
Random Numbers and Comparisons
csinschools.com
Previously, on CS in Schools...
2
Previously, on CS in Schools...
3
keep_going = True
if keep_going == True:
print("Hello!")
keep_going = True
while keep_going == True:
print("Hello!")
Previously, on CS in Schools...
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.
Learning Objectives
By the end of this lesson, you should be able to:
5
Python Reference Sheet
6
Python Reference Sheet
Demo: Guess My Age!
7
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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: | |
Activity: Guess My Height!
97
Activity 05.01�Guess My Height
Helpful Hints
Demo: Gimme Five!
98
Random numbers
99
Random numbers
100
Random numbers
101
Functions to the rescue!
102
For more information on modules and using other people's code, take a look here.
Functions to the rescue!
103
from random import *
rand_num = randint()
Functions to the rescue!
104
from random import *
rand_num = randint()
Functions to the rescue!
105
from random import *
rand_num = randint()
Functions to the rescue!
106
from random import *
rand_num = randint()
Functions to the rescue!
107
from random import *
rand_num = randint()
Functions to the rescue!
108
from random import *
rand_num = randint()
Functions to the rescue!
109
from random import *
rand_num = randint(10)
Functions to the rescue!
110
from random import *
rand_num = randint(10)
Functions to the rescue!
111
from random import *
rand_num = randint(10, 20)
Functions to the rescue!
112
from random import *
rand_num = randint(10, 20)
Notice we use a comma (,) to separate the two arguments
Functions to the rescue!
113
from random import *
rand_num = randint(10, 20)
Functions to the rescue!
114
from random import *
rand_num = randint(10, 20)
Functions to the rescue!
115
randint(10, 20)
from random import *
rand_num = randint(10, 20)
Functions to the rescue!
116
randint(10, 20)
12
from random import *
rand_num = randint(10, 20)
Functions to the rescue!
117
randint(10, 20)
12
from random import *
rand_num = randint(10, 20)
Functions to the rescue!
118
randint(10, 20)
12
from random import *
rand_num = randint(10, 20)
Functions to the rescue!
119
randint(10, 20)
12
from random import *
rand_num = randint(10, 20)
Functions to the rescue!
120
randint(10, 20)
12
rand_num
from random import *
rand_num = randint(10, 20)
Functions to the rescue!
121
randint(10, 20)
12
rand_num
12
from random import *
rand_num = randint(10, 20)
Functions to the rescue!
122
randint(10, 20)
12
rand_num
12
from random import *
rand_num = randint(10, 20)
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
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
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
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
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
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
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
Activity: Dance Off!
130
Activity 05.02�Dance Off
Helpful Hints
Activity: Guess My Number!
131
Activity 05.03�Guess My Number
Helpful Hints
Summary
132
Reflection: Exit pass
133
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