6. Datatypes cannot be mixed
your_age = int(input(“Your age?”))
my_age = 30
older_by = my_age – your_age
input function always returns a string. Convert
it to a number using the int (for integer)
function.
7. Dragon Game - Choice
import random
:::
good_cave = random.randint(1, 2)
print(“Which cave will you go into? (1 or 2)”)
chosen_cave = int(input())
8. Boolean
Can only take on one of two values –
True or False
is_programming_fun = True
is_cold = False
print(is_cold)
9. Boolean
Is my age same as yours?
The == operator checks if the values are equal
my_age = 30
your_age = ?
print(my_age == your_age)
11. Boolean
Am I older than you?
The > operator checks if LHS is greater than
RHS
my_age = 30
your_age = ?
am_i_older = my_age > your_age
print(am_i_older)
13. Conditionals
if always checks a Boolean variable for True
Has to be a Boolean
variable
if can_afford_both:
message = “You can afford both.”
else:
message = “You cannot afford both.”
15. Dragon Game - Result
import time
:::
print("You approach the cave...");
time.sleep(2)
print("It is dark and spooky...")
time.sleep(2)
print("A large dragon jumps out in front of you! He opens his jaws
and...")
print()
time.sleep(2)
if chosen_cave == good_cave:
print("Gives you his treasure")
else:
print("Gobbles you down in one bite!")
16. Loops
ctr = 0
while (ctr < 5):
print(“This is line ” + ctr)
ctr = ctr + 1
17. Loops
while (True):
print(“Stop this train, I wanna
get off!”)
print(“Oh! Thank goodness!”)
18. Dragon Game - play
import random
import time
play_again = "yes"
while (play_again == "yes"):
print("you are in a land full of dragons…")
::::
else:
print("Gobbles you down in one bite!")
play_again = input("Do you want to play again? (yes
or no)")
19. Sing and Talk
$ mpg123 /home/pi/viva.mp3 &
$ espeak “hello there”
>>> import os
>>> os.system(‘mpg123 /home/pi/viva.mp3 &’)
>>> os.system(‘espeak “hello there”’)
Ask students the output of the print statement
Ask the students to come up with more Boolean variables.
Ask students the output of the print statement
Ask the students to come up with more Boolean variables.
Ask students the output of the print statement
Explain the difference between = and ==
Ask students the datatype of the am_i_older variable (is it a String, Number, or Boolean?)
Note, can_afford_both is a Boolean variable that gets its value from an expression
Change the value of tablet_cost to 260 to give the concept of the >= operator
We can do a lot more inside if statements than simply print. Note that the if statement has an expression that evaluates to Boolean instead of a Boolean variable directly.
We can do a lot more inside if statements than simply print. Note that the if statement has an expression that evaluates to Boolean instead of a Boolean variable directly.
The last print statement will never print. This is an infinite loop because True will always be True.