Conditional Statements

 


Introduction

In this activity, you will gain practice with if statements, and will continue to improve your work with functions in Python.



Intro to If-else Statements

  1. In Google Colab, create a new notebook for today's activity. Give it a name indicating it has to do with experimenting with conditional statements.
  2. In a Code cell, write a function called testConditions1 that takes two parameters, say x and y, and does the following:
    If x is less than y, set x to 3. After the if statement it should print out the values of x and y.
  3. In a new Code cell, test your function by invoking it with different values for x and y. Make sure you choose appropriate values to test when the condition is true and when it is false.
  4. Write a function called testCondition2 that takes 2 parameters, say x and y, and does the following:
    if x equals 3 or y is not equal to 5, set x to 0; otherwise set x to -1. Then print the values of x and y at the end of your function. (Note: you can write this function in the same Code cell as the previous function, or you can create a new Code cell for it.)
  5. Test your function with several values of x and y that will make each condition true.

Time of Day Message Generator (If-elif-else statements)

In the following set of exercises, you will write a function that will print a different message based on the time of day. Your function should use a random number generator to determine the time of day, and to determine which message to display. The hours of the day go from 0 to 23, and should be broken into at least 3 different groups (such as 0 - 6, 6 - 12, 12 - 18, 18 - 23).

To generate random numbers, we will use the random module from Python's standard library. To use this module in your program, you must add the following statement at the top of your program:

import random
From the random module, we will use the randint function. This function takes two parameters that represent the range for which we want our random number and returns an integer within that range. For example, calling the randint function as in random.randint(1,10) will give us some number in the range of 1 to 10 (with 1 and 10 included in the range). You will need to generate two random numbers this way in your function. (See Section 8.2.1 of your text for more details of generating random numbers.)

The following instructions walk you through the steps for writing this function.

  1. In a new Code cell, add the statement that imports the random module.

  2. In the same Code cell, define a new function, dayMessage, that does not take any parameters.

  3. In the function, create a variable, randHour, that gets assigned a random number between 0 and 23. Your line of code should look something like the following:
    randHour = random.randint(0, 23)

  4. Print the value of this variable, along with text telling the user that this value is the hour.

  5. Use the if-elif-else statement (with as many elif blocks as you need) to print a different message for each time block.

  6. Test your function by invoking it enough times to see each message printed. Your output should look something like the following:
                The hour is: 13
                It's time for lunch!
                
    OR
                The hour is: 6
                It's too early!  Go back to sleep!
                
    OR
                The hour is: 22
                Get your homework done!
                

Choose your own Adventure (Nested If-else statements)

In these exercises, you will write a function that uses nested if/else statements o simulate a "choose your own adventure" style game. As an example, invoking a function called myGame (the one you write) might give output that looks something like the following:
 
You are standing in front of two doors.
Which would you like to open?

Enter 0 for left, or 1 for right.  0

Inside, a talking horse asks if you would like an apple.

Do you accept? Enter 0 for no or 1 for yes.  1

You win!  That apple was covered in caramel and was delicious!
This example is a two-level game. (The user answers two questions and gets a result.) To get you started, the first few lines of your function might look like the following:
def myGame():
    print("You are standing in front of two doors.")
    print("Which would you like to open?")
    door = int(input("Enter 0 for left, or 1 for right.\n "))
(Note: the \n at the end of the string passed to the input function causes the input to be typed on the next line.)

  1. Plan your adventure. To start with, the user should get two options (2 doors, 2 boxes, 2 roads, etc). Then within each of those two options, the user should get two choices. This means there are four possible outcomes overall. Decide what you want for these choices.
    For example, the adventure above starts with giving the user the choice between 2 doors. Inside one door was a talking horse. What you didn't see was that inside the other door was a talking goat. The horse offers the user an apple, and the goat would offer the user a candy bar. Based on whether or not the user accepts that offer, a different message would then get displayed.
     
  2. Write a function called myGame that presents two options to the user and then gets the user's input for which option they would like. (Your code may look similar to the myGame function above.)
     
  3. Add an if/else statement in your function that will print a different message based on the user's choice.
     
  4. Test your function. You should run your function twice, choosing a different option each time.
     
  5. Now, within each of the two user options, after you print the message, add some code asking the user to choose between two options related to this message. Test your code.
     
  6. Next, for each of these user inputs, add an if/else statement to print a message based on the user's choice.
     
  7. Test your code with different inputs to ensure all of your code executes correctly.
     
  8. Enhancements/Challenges 1: (For fun, not required) Modify your function so that it can handle incorrect input by the user. What should happen if the user enters in a number or letter other that what the program expects? (It probably prints out an error message and stops.)

  9. Enhancements/Challenges 2: (For fun, not required) Add another level to your adventure. How many possible outcomes will there be with three levels?

  10. Enhancements/Challenges 3: (For fun, not required) Write another version of your dayMessage function that randomly prints 1 of 2 or 3 possible messages for each time block. You will need a second random number in order to do this, as well as nested if-else statements.

Submit