This task is part of project04 which is due at 23:00 EDT on 2024-10-01.
You have the option to work with a partner on this task if you wish. Working with a partner requires more work to coordinate schedules, but if you work together and make sure that you are both understanding the code you write, you will make progress faster and learn more.
You can download the starter code for this task using this link.
You can submit this task using this link.
Put all of your work for this task into the file
animalQuiz.py
(you will create this file from scratch)
Note that this week, both tasks count separately towards the project credit. In other words, you need to do both of them, instead of just picking one.
What animal are you similar to? In this task, you will write a quiz program that determines your animal based on answers to three questions:
The answers to these three questions determine the animal shown in this table:
like meat? | like cold? | prefer fuzzy? | animal |
---|---|---|---|
True | True | True | polar bear |
True | True | False | orca |
True | False | True | tiger |
True | False | False | komodo dragon |
False | True | True | yak |
False | True | False | clam |
False | False | True | bunny |
False | False | False | tortoise |
Your task is to create the following four functions in a new file named
animalQuiz.py
:
boolFromResponse
- Accepts
one parameter (a string) and returns True
if that string begins
with 'Y'
or 'y'
, returning
False
in all other cases. The
.lower()
and
.startswith() methods of strings
are helpful for this function; using .startswith()
is
required, and
using .lower()
is an extra
goal. The sample
output gives examples of how
boolFromResponse
should work. boolFromResponse
must not call
input
, because
it only looks at its parameter.
boolFromUser
- Accepts one
parameter (a prompt string) and gathers input from the
user using that
prompt, before converting that input into a boolean using
boolFromResponse
,
and returning that boolean. The
sample output demonstrates how it should
workd.
chooseAnimal
- Accepts three
parameters, each a Boolean: the first indicates whether the user
likes meat or not, the second whether they like cold or not, and the
third whether they like fuzzy things or not. Based on these three
binary decisions, this function returns an animal
string based on the table shown above.
The sample output demonstrates how it should
work. Note that chooseAnimal
should not call
boolFromUser
:
it gets boolean values as parameters, and does not itself ask for
input. You must use one or more if
/else
statement(s) to select the
appropraite return value.
animalQuiz
- Does not accept any
parameters. It prompts the user with three yes/no questions, and then
displays the animal determined by their
answers. See the sample output
below for the exact wording you must
use. You must use the
boolFromUser
and
chooseAnimal
functions within this function, and you may not use input
directly (use
boolFromUser
instead).
One way to test your functions is to first run your program in the editor
window (to load the function definitions) and then enter sample calls of
your functions in the interactive console to test that they work
correctly. The behavior of each function should match the provided
examples (we have included examples for
boolFromResponse
,
boolFromUser
,
chooseAnimal
, and
animalQuiz
).
Interactive testing can be tedious, and it's easy to make mistakes if
you're testing things every time you make one little change. We have
provided a file called test_AnimalQuiz.py
which will test your
functions, although it does not check carefully enough to ensure that
they will satisfy all of our extra tests during grading.
You can work from the provided test cases in the starter file as examples, and you are encouraged to use the examples given in this file as the basis for your test cases. We encourage you define your test cases before writing your code, and just comment them out until you're ready to use them, because this will ensure that you have a clear idea of the correct behavior when you start writing each function.
.lower()
returns a lower-case version of the string
when called with zero arguments. The .startswith(s)
method takes a
single string as a parameter, and determines whether the string
before the period begins with the string it got as a parameter.
These examples show how these methods work. Note
that case matters for .startswith
.animalQuiz
, you must ask exactly the same questions in the
same order. Your printout should match the examples above. You can
use this difference checker website
to test for any minor differences in your output, and you could also
set up tests in optimism
for the correct output.startswith
and lower
Examples
These examples demonstrate how the .startswith
and .lower
methods work.
In []:Out[]:'Wendy Wellesley'.startswith('Wend')
In []:True
Out[]:'Wendy Wellesley'.startswith('Well')
In []:False
Out[]:'Wendy Wellesley'.startswith('W')
In []:True
Out[]:'Wendy Wellesley'.startswith('w')
In []:False
Out[]:'Wendy Wellesley'.lower()
In []:'wendy wellesley'
Out[]:'CS111'.lower()
In []:'cs111'
Out[]:'SHOUT!'.lower()
In []:'shout!'
Out[]:'the quick brown fox'.lower()
'the quick brown fox'
boolFromResponse
Examples
These examples demonstrate how boolFromResponse
should work. Note that it doesn't recognize words for 'yes' that don't start with 'y'.
In []:Out[]:boolFromResponse('yes')
In []:True
Out[]:boolFromResponse('Yes')
In []:True
Out[]:boolFromResponse('yup')
In []:True
Out[]:boolFromResponse('Yellow')
In []:True
Out[]:boolFromResponse('y')
In []:True
Out[]:boolFromResponse('No')
In []:False
Out[]:boolFromResponse('nope')
In []:False
Out[]:boolFromResponse('maybe')
In []:False
Out[]:boolFromResponse('affirmative')
False
boolFromUser
Examples
These examples demonstrate how boolFromUser
should work. Note that unlike boolFromResponse
, it gets input from the user instead of via a parameter.
In []:PrintsboolFromUser('Are you happy? ')
Are you happy? yesOut[]:In []:True
PrintsboolFromUser('Are you happy? ')
Are you happy? NOOut[]:In []:False
PrintsboolFromUser('Are you happy? ')
Are you happy? absolutely!Out[]:False
chooseAnimal
Examples
These examples demonstrate how chooseAnimal
should work. Note that user input is not involved.
In []:Out[]:chooseAnimal(False, False, True)
In []:'bunny'
Out[]:chooseAnimal(True, False, False)
In []:'komodo dragon'
Out[]:chooseAnimal(True, False, True)
'tiger'
animalQuiz
Examples
These examples demonstrate how animalQuiz
should work. Note that there are no parameters, and the user's answers determine the outcome.
In []:PrintsanimalQuiz()
What animal are you? Let's find out! Do you like to eat meat? Nope Do you like cold weather? yup! Do you like fuzzy things? y Your animal is the yakIn []:PrintsanimalQuiz()
What animal are you? Let's find out! Do you like to eat meat? unsure Do you like cold weather? maybe Do you like fuzzy things? absolutely Your animal is the tortoise
=
or by defining a parameter for a function) you must also later use that variable as part of another expression. If you need to create a variable that you won't use, it must have the name _
, but you should only do this if absolutely necessary.boolFromResponse
must return the correct result
boolFromResponse
function is run must match the solution result.boolFromResponse
must return the correct result
boolFromResponse
function is run must match the solution result.boolFromUser
must return the correct result
boolFromUser
function is run must match the solution result.boolFromUser
must return the correct result
boolFromUser
function is run must match the solution result.chooseAnimal
must return the correct result
chooseAnimal
function is run must match the solution result.animalQuiz
is approximately correct.
animalQuiz
with a variety of inputs, and examine everything that it prints, which should match what the solution code prints.boolFromResponse
with 1 parameter
def
to define boolFromResponse
with 1 parameterlower
boolFromResponse
with 1 parameter, call lower
in at least one place.boolFromResponse
with 1 parameter, do not use an if
statement (possibly accompanied by an elif
or else
block).boolFromResponse
with 1 parameter
def
to define boolFromResponse
with 1 parameterinput
boolFromResponse
with 1 parameter, do not call input
..startswith
or use indexing
boolFromResponse
with 1 parameter, you must either call the .startswith
string method or use indexing to check the first letter of the response.boolFromUser
with 1 parameter
def
to define boolFromUser
with 1 parameterinput
boolFromUser
with 1 parameter, call input
in at least one place.boolFromResponse
boolFromUser
with 1 parameter, call boolFromResponse
in at least one place.chooseAnimal
with 3 parameters
def
to define chooseAnimal
with 3 parametersboolFromUser
chooseAnimal
with 3 parameters, do not call boolFromUser
or boolFromResponse
.chooseAnimal
with 3 parameters, use an if
statement (possibly accompanied by an elif
or else
block) in at least one place.animalQuiz
def
to define animalQuiz
boolFromUser
animalQuiz
, call boolFromUser
in exactly 3 places.chooseAnimal
animalQuiz
, call chooseAnimal
in exactly one place.animalQuiz
def
to define animalQuiz
input
animalQuiz
, do not call input
.boolFromUser
animalQuiz
, call boolFromUser
in at least one place.chooseAnimal
animalQuiz
, call chooseAnimal
in at least one place.