if-elif-else Statements Exercises

  1. Syntax for if Statements:
    if condition:
        indentedStatementBlockForTrueCondition
    
  2. Syntax for if-else Statements:
    if condition:
        indentedStatementBlockForTrueCondition
    else:
        indentedStatementBlockForFalseCondition
    
  3. Syntax for if-elif-else Statements:
    if condition1:
        indentedStatementBlockForTrueCondition1
    elif condition2:
        indentedStatementBlockForTrueCondition2
    elif condition3:
        indentedStatementBlockForTrueCondition3
    elif condition4:
        indentedStatementBlockForTrueCondition4
    else:
        indentedStatementBlockForAllFalseCondition
    
  4. How else can we write the if-elif-else statements below (without using an elif)?
    # Given an age from the user and whether or not they're showing significant signs of independence,
    # display if the person is a teenager, acting like one or is neither
    import sys
    
    age = int(input("Please enter the age: "))
    
    userResponse = input("Is the person showing significant signs of independence? (yes/no) ").lower()
    if userResponse == "yes":
        showingSignsOfIndependence=True
    elif userResponse == "no":
        showingSignsOfIndependence=False
    else:
        print("ERROR: Please answer yes or no!")
        sys.exit(1)
    
    if (age >= 13) and (age <= 19):
        print("Teenager")
    elif (age < 13) and showingSignsOfIndependence == True:
        print("Acting like a teenager")
    else:
        print("Not a teenager")
    
  5. Animal Guessing Game:
    Extend the following game by adding in more questions and guesses:
    rightMsg = "Yeah, that was fun!"
    wrongMsg = "What was it and what question could extend this game?"
    
    print("Think of an animal")
    
    response = input("Does it fly? (yes/no) ")
    if response == "yes":
        response = input("Is it an eagle? (yes/no) ")
        if response == "yes":
            print(rightMsg)
        else:
            print(wrongMsg)
    else:
        response = input("Does it run really fast? (yes/no) ")
        if response == "yes":
            response = input("Is it a cheetah? (yes/no) ")
            if response == "yes":
                print(rightMsg)
            else:
                print(wrongMsg)
        else:
            print(wrongMsg)
    
  6. Write a complete python script that asks the user for their overall grade percentage in this class. Display the appropriate letter grade.
    Example:
    Please enter your overall grade (ex: 98.7): 91
    91.0% is an 'A'
  7. Loops and branching
    1. Write a complete python script that displays the following grade information:
      59% is an 'F'
      60% is a 'D'
      61% is a 'D'
      62% is a 'D'
      63% is a 'D'
      64% is a 'D'
      65% is a 'D'
      66% is a 'D'
      67% is a 'D'
      68% is a 'D'
      69% is a 'D'
      70% is a 'C'
      71% is a 'C'
      72% is a 'C'
      73% is a 'C'
      74% is a 'C'
      75% is a 'C'
      76% is a 'C'
      77% is a 'C'
      78% is a 'C'
      79% is a 'C'
      80% is a 'B'
      81% is a 'B'
      82% is a 'B'
      83% is a 'B'
      84% is a 'B'
      85% is a 'B'
      86% is a 'B'
      87% is a 'B'
      88% is a 'B'
      89% is a 'B'
      90% is an 'A'
      91% is an 'A'
      92% is an 'A'
      93% is an 'A'
      94% is an 'A'
      95% is an 'A'
      96% is an 'A'
      97% is an 'A'
      98% is an 'A'
      99% is an 'A'
      100% is an 'A'
    2. Now extend your script to add in a blank line between each letter grade:
      59% is an 'F'
      	    
      60% is a 'D'
      61% is a 'D'
      62% is a 'D'
      63% is a 'D'
      64% is a 'D'
      65% is a 'D'
      66% is a 'D'
      67% is a 'D'
      68% is a 'D'
      69% is a 'D'
      
      70% is a 'C'
      ...