day3

20
Welcome Avilay Parekh Anika Parekh @avilay

Upload: avilay-parekh

Post on 06-Jul-2015

295 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Day3

Welcome

Avilay Parekh

Anika Parekh

@avilay

Page 2: Day3

Datatypes cannot be mixedage = 30print(name + “is” + age + “years old”)

Ka-Boom!!

Page 3: Day3

Datatypes cannot be mixed

age = 30print(name + “is” + str(age) + “years old”)

Convert the number to a string using the str function

Page 4: Day3

Datatypes cannot be mixed

age = “30”print(name + “ is ” + age + “ years old ”)

Or just declare the variable age as a string.

Page 5: Day3

Datatypes cannot be mixed

your_age = input(“Your age?”)my_age = 30older_by = my_age – your_age

Ka-Boom!!

Page 6: Day3

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.

Page 7: Day3

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())

Page 8: Day3

Boolean

is_programming_fun = Trueis_cold = Falseprint(is_cold)

Can only take on one of two values –True or False

Page 9: Day3

Boolean

my_age = 30your_age = ?print(my_age == your_age)

Is my age same as yours?The == operator checks if the values are equal

Page 10: Day3

Boolean

my_age = 30your_age = ?print(my_age = your_age)

Sneaky Bug!

Page 11: Day3

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

Page 12: Day3

Boolean

money = 500phone_cost = 240tablet_cost = 200total_cost = phone_cost + tablet_costcan_afford_both = money > total_costprint(can_afford_both)

Page 13: Day3

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

Page 14: Day3

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

?

Page 15: Day3

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!")

Page 16: Day3

Loops

ctr = 0while (ctr < 5):

print(“This is line ” + ctr)ctr = ctr + 1

Page 17: Day3

Loops

while (True): print(“Stop this train, I wanna

get off!”)

print(“Oh! Thank goodness!”)

Page 18: Day3

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)")

Page 19: Day3

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”’)

Page 20: Day3

Conditionals

Loops

Making the Pi

sing and talk

Module 3