# More examples of if statements and while loops. # The following program asks a number of questions to guess what animal # the user is thinking about. The aim is to pinpoint the answer by # asking the user as few questions as possible. Animals = "tiger,horse,wolf,deer,shark,whale,seal,turtle" print("think of one of the following animals and I will try to guess which it is:",Animals) answer = input("Does the animal live on land? ") if (answer[0] == 'y'): # if the first letter of the answer is y (yes) answer = input("Is it a carnivore? ") if (answer[0]=='y'): # land carnivore answer = input("Does it hunt in packs? ") if (answer[0]=='y'): print("The animal is a wolf") else: print("The animal is a tiger") else: #land non-carnivore answer = input("Is it a farm animal? ") if (answer[0]=='y'): print("The animal you're think of is a horse") else: print("The animal is a deer") else: # does not live on land answer = input("Is it a mammal? ") if (answer[0]=='y'): # aquatic mammal answer=input("Does it have fur? ") if (answer[0]=='y'): print("The animal is a seal") else: print("The animal is a whale") else: #aquatic non-mammal (shark or turtle) answer = input("Does have have a shell? ") if (answer[0]=='y'): print("The animal is a turtle") else: print("The animal is a fish") ############# # Nev Erstudy is a student at Hofstra who always keeps the following schedule: # Weekdays between 2pm and 5pm: in class # Everyday before 12pm: asleep # Everyday 12pm-2pm: lunch # Weekdays after 5pm, or Weekends after 2pm: partying until 3am # The input of this program will be day = int(input("Enter day of the week, 0=Sunday: ")) # day is between 0 and 6 hour = int(input("Enter hour of day, 0-23: ")) # 0 is midnight # Write a program to determine and print out what Nev is doing at this time. # Each integer hour value is intended to last until the end of the hour, # e.g. 12 represents 12:00 to 12:59 weekday = (day>0 and day<6) # monday-friday asleep = (hour>=3 and hour<12) lunch = (hour==12 or hour==13) if asleep: print("Nev is asleep") if lunch: print("Nev is having lunch") """ # there's mistake in the following code, why? if weekday and hour>=2 and hour <5: print("Nev is in class") else: print("Nev is partying") """ inclass = weekday and hour>=14 and hour<17 if inclass: print("Nev is in class") if not(inclass or lunch or asleep): print("Nev is at a party") # You can also use elif: # if asleep: print("Nev is sleeping") # elif lunch: ... # elif weekday and hour>=2 and hour <5: ... # else: print("Nev is at a party") ## Modify Nev's schedule so that on Fridays between 5pm and 10pm, Nev's # at the movies, and on Sundays from 5pm to 9pm, Nev is having dinner # at mother's house. #### One final practice problem (EXERCISE) with if-else statements. # Let's go to beautiful New Jersey! Interact with native New Jersians # and learn about their unique culture. Driving from Long Island to # New Jersey could be difficult. The best route to get there # depends on the day of week and time of the day. Weekday rush # hours are assumed to be from 6am to 10am and from 4pm to 8pm. # However, on Fridays, evening rush hour are from 3pm to 8pm. The best # time to go would be Saturday or Sunday morning, when traffic is light. # The routes we will consider are: the Lincoln Tunnel, Holland Tunnel, # the Verrazzono Narrows Bridge, and the George Washington Bridge. The # ALGORITHM is as follows. # On Weekends before 10am (<=10), take the Lincoln Tunnel # On Weekends after 10am, take the Verrazzono Bridge # During weekday non-rush hours, take the Holland Tunnel # During weekday rush hours, take the George Washington bridge (and be patient). day = int(input("Enter day of the week, 0=Sunday, 1=Monday ... 6=Saturday")) hour = int(input("Enter hour of the day, 0-23")): if (day<0 or day>6 or hour<0 or hour>23): raise Exception("invalid input") ###################### #Sample while loops # print all odd numbers from 1 to 100 backwards n = 99 # starting number while n>0: if n%2==1: print(n,"is odd") n = n-1 # or just n=n-2 without the test. # end while; this comment is required if while loop contains more than # 2 statements in its body. # generate random number, ask user for guess until user gets it right, # counts number of guesses: from random import randint # built-in function generates random integers num = randint(1,128) # random number from 1 to 128, inclusive print("Guess my number, which is between 1 and 128") guess = num+1 # start with wrong guess tries = 0 # counts number of tries while num != guess and guess!=0: guess = int(input("What's your guess? (0 to give up): ")) tries = tries +1 # equivalent to tries += 1 if (guess!=0 and guess!=num): print("Wrong!") if (guess<0 or guess>127): print("Ha Ha Ha!") #while wrong guess if guess==0: print("Quitter!") else: print("You guessed the number in",tries,"tries") # modify program to give player option to quit ... # modify program to give user a chance ...