Python代写|CSSE1001/CSSE7030 Recipes and Ingredients: Shopamania COOK1001


1 Introduction

You are a single parent with four hungry children and the weekly grocery shop is becoming difficult. Luckily, you know a bit of Python and can write a program that generates a shopping list based on what recipes are going to be prepared that week.

2 Getting Started

Download from Blackboard — this archive contains the necessary files to start this assignment. Once extracted, the archive will provide the following files: This is the only file you will submit and is where you write your code. Do not make changes to any other files. Do not modify or submit this file, this file also contains some constants that will be useful in your implementation. In addition to these, you are encouraged to create your own constants in where possible. This is where you will find some recipes that can be added to the recipe book as well.

Note: You are not permitted to add any import statements to Doing so will result in a deduction of up to 100% of your mark.

3 Terminology

In the context of this assignment:

recipe name: str represents the name of a recipe. Note: Recipe names should only contain lowercase letters from the English alphabet without any numbers or special characters.

recipe: tuple[str, str] is a tuple containing two str’s. The first str in the tuple represents the name of the recipe. The second str contains all the ingredients in the form of ‘amount measure ingredient_name’, e.g.

‘115 g Nuttelex’ which represents 115

grams of Nuttelex. When multiple ingredients are present, they are separated by a single comma. Note that there is no space. e.g. ‘115 g Nuttelex,50 g sugar’ have a look at for examples.

recipes: list[tuple[str, str]] is a list of recipes. Each recipe follows the format described above. Duplicates can exist.

ingredient detail: tuple[float, str, str] , is a tuple containing information of a single ingredient. (amount, measure, ingredient name), e.g. (115.0, ‘g’, ‘Nuttelex’).

cookbook is a collection of all available recipes and you may use whatever type you feel is appropriate for it. Duplicates do not exist in the cookbook.

4 Interaction

This section provides an overview of the interaction. Where prompts and outputs are not explicitly mentioned in this section, please see Section 5.

4.1 Interaction loop

At the beginning of the interaction, the user is prompted with the message

Please enter a command:

(note the trailing space) to choose a command. Once a command is entered the user should be prompted again. The valid commands are outlined in Table 1.

Throughout the interaction, the user may add to or remove from the cookbook. They can also add to or remove from the list of recipes as well. You may assume that your program will not be tested for invalid inputs that do not match the expected commands in Table 1. The prompt should repeat until the user quit by entering the “Q” or “q” action.

Table 1: Potential command that user can choose. {recipe} denotes that the name of recipe should be provided. Values not surrounded by braces should be taken as string literals.

5 Implementation

This section outlines functions you are required to implement in your solution (in only).

You are awarded marks for the number of tests passed by your functions when they are tested independently. Thus an incomplete assignment with some working functions may well be awarded more marks than a complete assignment with faulty functions. Your program must operate exactly as specified. In particular, your program’s output must match exactly with the expected output.

Your program will be marked automatically so minor differences in output (such as whitespace or casing) will cause tests to fail resulting in a zero mark for that test.

Each function is accompanied with some examples for usage to help you start your own testing.

You should also test your functions with other values to ensure they operate according to the descriptions.

5.1 Required Functions

The following functions must be implemented in They have been listed in order of increasing difficulty. It is highly recommended that you do not begin work on a later function until each of the preceding functions can at least behave as per the shown examples. You may implement additional functions if you think they will help with your logic or make your code easier to understand.

Your first task should be writing function headers for each of these required functions into

For example, you could write

def get_recipe_name(recipe: tuple[str, str]) -> str

“”” Return …

>>> get_recipe_name(…)

in your file and then add usage examples and docstrings based on what you read below. Then completing the assignment means filling in each function properly as is done with ShiFoo.


num hours() -> float

This function should return the number of hours you estimate you spent (or have spent so far) on the assignment, as a float. You will not be marked differently for spending more or less time on the assignment. The purpose of this function is to enable you to verify that you understand how to submit to Gradescope as soon as possible, and to allow us to gauge difficulty level of this assignment in order to provide the best possible assistance.

Ensure this function passes the relevant test on Gradescope as soon as possible. If the Gradescope tests have been released, you must ensure this function passes the relevant test before seeking help regarding Gradescope issues for any of the later functions. See Section 7.3 for instructions on how to submit your assignment to Gradescope.


get recipe name(recipe: tuple[str, str]) -> str

Returns the name of the recipe.

>>> get_recipe_name((‘chocolate peanut butter banana shake’,

‘1 large banana,240 ml almond milk’))

‘chocolate peanut butter banana shake’

>>> get_recipe_name((‘cinnamon rolls’,

‘480 ml almond milk,170 g Nuttelex’))

‘cinnamon rolls’


parse ingredient(raw ingredient detail: str) -> tuple[float, str, str]

Return the ingredient breakdown from the details amount, measure, and ingredient.

>>> parse_ingredient(‘0.5 tsp coffee granules’)

(0.5, ‘tsp’, ‘coffee granules’)

>>> parse_ingredient(‘1 large banana’)

(1.0, ‘large’, ‘banana’)


create recipe() -> tuple[str, str]

Return a recipe in the tuple[str, str] format after a series of prompting. The recipe name is prompted first followed by continuous ingredient prompting until an empty string is entered (enter or return key press with no text).

>>> create_recipe()

Please enter the recipe name: peanut butter

Please enter an ingredient: 300 g peanuts

Please enter an ingredient: 0.5 tsp salt

Please enter an ingredient: 2 tsp oil

Please enter an ingredient:

(‘peanut butter’, ‘300 g peanuts,0.5 tsp salt,2 tsp oil’)


recipe ingredients(recipe: tuple[str, str]) -> tuple[tuple[float, str, str]]

Return the ingredients of a recipe amount, measure, and ingredient. This transforms a given recipe from the string form into the tuples form.

>>> recipe_ingredients((‘peanut butter’,

‘300 g peanuts,0.5 tsp salt,2 tsp oil’))

((300.0, ‘g’, ‘peanuts’), (0.5, ‘tsp’, ‘salt’), (2.0, ‘tsp’, ‘oil’))


add recipe(new recipe: tuple[str, str],

recipes: list[tuple[str, str]]) -> None

Add a given recipe, new_recipe, into the list of recipes. Hint: This function doesn’t return anything. Recall list mutability.