In this activity, you will gain practice with if statements, and will continue to improve your work with functions in Python.
testConditions1
that takes
two parameters, say x
and y
, and does the
following: if
statement it should print out the values of x and
y.testCondition2
that
takes 2 parameters, say x
and y
, and does
the following: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 randomFrom 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.
random
module.
dayMessage
, that
does not take any parameters.
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)
if-elif-else
statement (with as
many elif
blocks as you need) to print a different
message for each time block.
ORThe hour is: 13 It's time for lunch!
ORThe hour is: 6 It's too early! Go back to sleep!
The hour is: 22 Get your homework done!
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:
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: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!
(Note: the \n at the end of the string passed to the input function causes the input to be typed on the next line.)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 "))
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.)
if/else
statement in your function that will print a
different message based on the user's choice.if/else
statement to print a message based on the user's
choice.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.