If Statements
Introduction
In this mini-lab you will gain practice with if statements, and will
continue to improve your work with
functions in Python.
Intro to If Statements
- In the
Editor
in Spyder, create a new file for the functions you will
write in
this mini-lab. Then save the new file with a name
representative of this minilab.
- 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. Then print out the values of x and
y.
- Test your function by calling 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.
- 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.
- Test your function with several values of x and y that will make
each condition true.
Choose your own Adventure (If-else statements)
In these exercises, you will use nested if/else
to write a
"choose your own adventure" style game. As an example, a session of the
game might look something like:
In[1]: myGame()
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 my function 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.)
- 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.
- 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.)
- Add an
if/else
statement in your function that will print a
different message based on the user's choice.
- Test your function. You should run your function twice,
choosing a different option each time.
- 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.
- Next, for each of these user inputs, add an
if/else
statement to print a message based on the user's
choice.
- Test your code with different inputs to ensure all of your code
executes correctly.
- 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.)
- Enhancements/Challenges 2: (For fun, not required) Add
another level to your adventure. How many possible outcomes will there
be with three levels?
Submit
When you are done, update the description of the program in the
comments at the top of your file, and then submit the file you created for this mini-lab via
Kit. This file should have at least three functions in it.