Due date: Thursday, November 14, 11:59pm
According to data from Feeding America, 11% of people in Madison county (which is where Hamilton is located) faced food insecurity in 2022. The Hamilton Food Cupboard is a critical community resource for individuals facing food insecurity. They operate on a client choice food pantry model, in which individuals can choose what products they receive, just like shopping at a grocery store.
To make the Hamilton Food Cupboard even more convenient and welcoming to community members needing food assistance, the Food Cupboard has (fictitiously) decided to offer food pick-up/delivery service similar to companies like Mercato. Your task is to write a program that clients can use to select which foods they would like to receive from the Food Cupboard.
Please write your code in the template file
hw7_mercato.py
included with this homework. The file
includes an almost empty main
function that contains some
variables that have been pre-populated for your program to use
initially.
Your first task is to incrementally write and test two fruitful
functions. As you did in the lastest lab, we want you to practice
test-driven development by using doctests
that guide each function’s implementation. Hence, you must write each
appropriate doctest
before adding the code that covers this
particularity. Refer to your last lab implementation as a reference to
include the relevant import
and the call to execute the
doctests
in hw7_mercato.py
where you will
complete these two stand-alone functions.
getUnique
First, create and implement a function called getUnique
that takes a list of strings and returns a new list that contains only
the unique items from the argument list in the order they originally
appear. The argument list should not be modified. For
example:
getUnique(["A", "B", "CD", "A", "A", "CD", "efg"])
should return a new list containing
['A', 'B', 'CD', 'efg']
filter
Similarly, add a function called filter
that takes two
list of strings, listA
and listB
, as well as a
target string, target
. This function returns a
new list containing elements out of listB
where the corresponding index of listA
is equal to
target
. The argument lists should not be modified.
For example:
filter(["FY", "Soph", "FY", "Junior", "Senior", "FY"], ["COS101", "COSC202", "COS102", "COSC315", "COSC435", "FSEM135"], "FY")
should return a new list containing
["COS101", "COS102", "FSEM135"]
.
This is because, "FY"
(i.e. target
)
appears at indices 0
, 2
, and 3
of
listA
, so the returned list contains the corresponding
values at these indices in listB
)
filter(["FY", "Soph", "FY", "Junior", "Senior", "FY"], ["COS101", "COSC202", "COS102", "COSC315", "COSC435", "FSEM135"], "Grad")
should return an empty list []
Once you have implemented, debugged, and fully tested through
doctests
on these two generic functions you are ready to
tackle the design and implementation for the next problem.
Do not forget these two functions, they must be used to solve your Mercato program.
Next, you need to implement the Food Mercato program according to the
specifications below. The trace1.pdf
,
trace2.pdf
and trace3.pdf
files provided with
this homework demonstate what a correctly working program should look
like. Study these examples closely to determine the patterns that help
you design and structure your program.
The execution of your program should begin by displaying a welcome banner:
_____ ,_ _
() |_ _ _ _| /| | | _ ,_ _ _, _|_ _
/| |/ \_/ \_/ | | | | |/ / | / / | | / \_
(/ \_/ \_/ \/|_/ | | |_/|_/ |/\__/\/|_/|_/\_/
Working in the main
function in
hw7_mercato.py
, you should utilize our provided function to
display this welcome banner.
Next, the program should display the unique categories of
food from which a client can select. The categories should be
displayed in the order they first occur in the
categories
list included in the provided
main
and should be numbered starting at 1.
After listing the categories, the program should ask the user:
Which category do you want?
Categories:
1. Fruit
2. Vegetable
3. Grain
4. Dairy
Which category do you want?
If the client enters a non-numeric value or a number that is
not one of the displayed options, then the program should
print the message Enter a number between 1 and _
(replacing _
with the largest possible number) and ask the
user again: Which category do you want?
. The
process should repeat until the user enters a valid choice. For
example:
Which category do you want? F
Enter a number between 1 and 4
Which category do you want? Fruit
Enter a number between 1 and 4
Which category do you want? 10
Enter a number between 1 and 4
Which category do you want? 1
Remember the string function "13".isdigit()
– it will be
useful to solve part of this input validation!
Next, the program should display the available foods in the
selected category. The foods
list included in the
hw7_mercato.py
includes foods across all categories. A
food’s category is determined by filtering the food
list
using the categories
list. For example,
foods[1]
is 'Broccoli'
and
categories[1]
is 'Vegetable'
. Similar to
above, the foods should be displayed in the order they occur in
the foods
list and should be numbered
starting at 1. After listing the foods, the program should
ask the client:
Which food do you want?
For example, if the user chose the ‘Fruit’ category, the program would output the following:
Foods:
1. Bananas
2. Pears
Which food do you want?
As before, if the client enters a non-numeric value or a
number that is not one of the displayed options, then the
program should print the message
Enter a number between 1 and _
(replacing _
with the largest possible number) and again ask the user
Which food do you want?
Next, the program should display the client’s remaining credits for selecting food. In the client choice food pantry model, clients are normally given a credit budget based on a number of factors (e.g., household size), and each item is assigned a “cost” in credits. For simplicity, your program will assume a client starts with 10 credits and each unit of a food costs one credit regardless of the category or specific food.
The program should ask the client:
How many units of 'xxxx' do you want?
,
xxx
is replaced by the name of food item previously
selected. If the client enters a non-numeric value or a number
greater than their remaining credits, then the program should
print the message Enter a number between 1 and _
(replacing _
with the number of remaining credits) and
again ask the user How many units do you want?
For example:
Remaining credits: 10
How many units of 'Pears' do you want? 12
Enter a number between 1 and 10
How many units of 'Pears' do you want? five
Enter a number between 1 and 10
How many units of 'Pears' do you want? 5
The number of units should be deducted from the client’s remaining credits. If the client still has remaining credits, then the client should again be asked to select a category, select a food, and enter the desired number of units.
After the client has used all of their credits, the program should display the foods they requested and the number of each item using the following format:
Foods requested:
+ Milk (x6)
+ Cauliflower (x4)
The items must be displayed in the order they were selected by the client.
Note that a client may request some units of an item, then request more units of that same item, but the item should only appear once in the order summary with the total amount requested.
For example:
Categories:
1. Fruit
2. Vegetable
3. Grain
4. Dairy
Which category do you want? 3
Foods:
1. Wheat bread
2. Walnuts
Which food do you want? 2
Remaining credits: 10
How many units of 'Walnuts' do you want? 6
Categories:
1. Fruit
2. Vegetable
3. Grain
4. Dairy
Which category do you want? 3
Foods:
1. Wheat bread
2. Walnuts
Which food do you want? 2
Remaining credits: 4
How many units of 'Walnuts' do you want? 4
Foods requested:
+ Walnuts (x10)
For this homework, you must decide how to structure your program (you may want to use a flow chart to guide your implementation).
It is fundamental that your program
If you find yourself copying and pasting (nearly) the same code, you should create a function that can be called from multiple points in your program!
For all functions you write, you should ensure:
Write a little bit of code at a time (one function at most – but maybe just a few lines in a function) and test as you go. Writing most or all of the code you think you need then testing afterward is a terrible strategy that will lead to frustration. Do a little bit at a time!
Remember to document each of your functions with annotations,
docstring
s, and doctest
s.
Once you have implemented all of the above functionality, you must
make your program work with categories
and
foods
lists that are different than those originally
provided in hw7_mercato.py
. These alternate lists
will be read in from an external file, where each line
of the file will contain a single food item and its respective
category – example below:
Frozen Quiche
Produce Oranges
Canned Chickpeas
Frozen Gelato
Canned Beans
Canned Olives
Produce Mangoes
Produce Grapes
After displaying the welcome banner, your program should now ask the
user if they wish to use the foods and categories contained in an
external food_data.txt
file. If the user responds with
Y
/y
, the foods and categories are determined
by the contents of food_data.txt
, otherwise the default
lists are used.
Your program does not need to account for a missing
or improperly formatted food_data.txt
file. Below is an
example:
_____ ,_ _
() |_ _ _ _| /| | | _ ,_ _ _, _|_ _
/| |/ \_/ \_/ | | | | |/ / | / / | | / \_
(/ \_/ \_/ \/|_/ | | |_/|_/ |/\__/\/|_/|_/\_/
Do you wish to read in food data from food_data.txt?
Enter (Y) to read from a file, or any other input to use default values: Y
Categories:
1. Frozen
2. Produce
3. Canned
Which category do you want? 1
Foods:
1. Quiche
2. Gelato
Which food do you want? 1
...
When we are assessing your code, higher levels of achievement are demonstrated, in part, by
Submit your Python file, hw7_mercato.py
, to the platform
indicated in your class section.
Remember to complete the questions at the top of the
hw7_mercato.py
file before submitting.