day3
TRANSCRIPT
Welcome
Avilay Parekh
Anika Parekh
@avilay
Datatypes cannot be mixedage = 30print(name + “is” + age + “years old”)
Ka-Boom!!
Datatypes cannot be mixed
age = 30print(name + “is” + str(age) + “years old”)
Convert the number to a string using the str function
Datatypes cannot be mixed
age = “30”print(name + “ is ” + age + “ years old ”)
Or just declare the variable age as a string.
Datatypes cannot be mixed
your_age = input(“Your age?”)my_age = 30older_by = my_age – your_age
Ka-Boom!!
Datatypes cannot be mixed
your_age = int(input(“Your age?”))my_age = 30older_by = my_age – your_age
input function always returns a string. Convert it to a number using the int (for integer) function.
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())
Boolean
is_programming_fun = Trueis_cold = Falseprint(is_cold)
Can only take on one of two values –True or False
Boolean
my_age = 30your_age = ?print(my_age == your_age)
Is my age same as yours?The == operator checks if the values are equal
Boolean
my_age = 30your_age = ?print(my_age = your_age)
Sneaky Bug!
Boolean
my_age = 30your_age = ?am_i_older = my_age > your_ageprint(am_i_older)
Am I older than you?The > operator checks if LHS is greater than RHS
Boolean
money = 500phone_cost = 240tablet_cost = 200total_cost = phone_cost + tablet_costcan_afford_both = money > total_costprint(can_afford_both)
Conditionals
if can_afford_both:message = “You can afford both.”
else:message = “You cannot afford both.”
if always checks a Boolean variable for True
Has to be a Boolean variable
Conditionals
your_age = int(input(“Your age?”))my_age = 30older_by = -1if my_age > your_age:
older_by = my_age – your_ageelif my_age < your_age:
older_by = your_age – my_age else:
older_by = 0
?
Dragon Game - Resultimport 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!")
Loops
ctr = 0while (ctr < 5):
print(“This is line ” + ctr)ctr = ctr + 1
Loops
while (True): print(“Stop this train, I wanna
get off!”)
print(“Oh! Thank goodness!”)
Dragon Game - playimport randomimport 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)")
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”’)
Conditionals
Loops
Making the Pi
sing and talk
Module 3