External Exam Download Resources Web Applications Games Recycle Bin

Branching ELIF

elif simplest.py

if "this" == "that":
  print("branch 1")
elif 5 < 0:
  print("branch 2")
elif 1 == 2:
  print("branch 3")
elif "matching" == "matching": #because this is True,
  print("branch 4") # script will exit on this branch
else:
  print("branch for everything else")


elif simpler.py

animal = input("(c) for cat, (d) for dog, (m) for moo, (f) for fox: ")

if animal == "c":
  print("Meow") 
elif animal == "d":
  print("Woof")
elif animal == "m":
  print("Moo") 
else:
  print("What does the fox say?")


elif simple.py

score = 15

if score < 5: #4 or lower
  print("Low score.") 
elif 5 <= score <= 25: #between 5 and 25 inclusive
  print("Standard score.") 
else: #26 or higher 
  print("High score.")


elif.py

sprintTime = float(input("What is your fastest 100m time?")) 

if sprintTime < 10.0: 
  print("You are an Olympic sprinter.") 
elif 10.0 <= sprintTime <= 13.0: 
  print("You are an excellent sprinter.") 
else: 
  #not a sprinter, check for distance running instead: 
  print("Perhaps you should try distance running.") 
  distance = int(input("How many kilometers can you run?")) 

  if distance <= 1: 
    print("You are a short to middle distance runner.") 
  elif distance > 1 and distance < 10: 
    print("You are a middle to long distance runner.") 
  elif distance >= 10 and distance <= 42: 
    print("You are a long distance runner.") 
  else: 
    print("You are an ultra marathon runner.")


  1. Write a program that asks a user the colour of a traffic light. If it is a "green" light, output "go". If it is a "yellow" light, output "wait". If it is a "red" light, output "stop".

  2. generate a random number between 1 and 5 and store it in a variable SECRETNUM. user gets 1 guess to get it right. to do this, input an integer from the user into a variable named GUESSNUM. If the GUESSNUM is higher than the SECRETNUM, say "you lost. secret num was higher than your guess". Say the opposite if it is lower. Finally, say "you won!" if they guess it first go.

  3. Write a program that reads in a user's age (as an integer). If the user is under 12, the program should say "You need your parents to apply for a TFN." If the user is 70 or over, the program should say "You could retire." If the user is 13 or older, but younger than 70, the program should ask "Are you currently working?", to which the user responds "Yes" or "No"...

    ...If the user responds "Yes", the program says "Keep working hard." If the user responds "No", the program says "Enjoy your time off."

  4. Write a program that generates a random card from a deck of cards, and writes the text of the card to the screen. To do this you will need two random numbers - one for the card weight (between 1-13), and one for the card suit(between 1-4). To cut down on your coding, it is OK to use the integer value of cards 2-10, but for 11, 12, 13 and 1, the cards should say "Jack", "Queen", "King" and "Ace" respectively. Some valid examples of output include:
    The 2 of Clubs 
    The 6 of Diamonds 
    The Ten of Hearts 
    The Ace of Spades

  5. For the previous set of questions about crafting pickaxes or hatchets (axes) here, use an indented / nested if statement to determine how many materials are lacking (i.e. short) if i cannot afford to craft the item. For example, your program should print "Cannot afford pickaxe, you still require 2 sticks and 1 cobblestones."