Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

********************************PLEASE USE Python******************** cookies = Recipe(cookies, {cookie jar : 1}, [take a cookie out of the jar]) print(cookies.title) print(cookies.ingredients) print(cookies.directions) cookies {'cookie jar': 1} ['take

********************************PLEASE USE Python********************image text in transcribedimage text in transcribed

cookies = Recipe("cookies", {"cookie jar" : 1}, ["take a cookie out of the jar"]) print(cookies.title) print(cookies.ingredients) print(cookies.directions)
cookies {'cookie jar': 1} ['take a cookie out of the jar']

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

print(cookies)
# printed output How To Make Cookies ------------------------------- Ingredients Flour (grams) : 400 Butter (grams) : 200 Salt (grams) : 10 Sugar (grams) : 100 Directions 1. In a large mixing bowl, cut the chilled butter into the flour and sugar. 2. Add the salt and sugar, and combine. 3. Roll into a log, and freeze. 4. Preheat the oven to 200 C. 5. Cut the log of dough into thin disks and place on baking sheet. 6. Bake for 12 minutes, flipping after 7 minutes.

Introduction In this worksheet, you'll implement a recipe class that stores data on how to create a tasty dish--like cookies! -in a structured way. We'll build on this recipe class in a future Discussion activity. Schematically, a recipe has three primary pieces of data: 1. A title (e.g. "Cookies") 2. A list of ingredients, with quantities. Example: Flour (grams) : 400 Butter (grams) : 200 Salt (grams) : 10 Sugar (grams) : 100 3. A list of directions. Example: 1. In a large mixing bowl, cut the chilled butter into the flour and sugar. 2. Add the salt and sugar, and combine. 3. Roll into a log, and freeze. 4. Preheat the oven to 200 C. 5. Cut the log of dough into thin disks and place on baking sheet. 6. Bake for 12 minutes, flipping after 7 minutes. Our recipe class will store such data. In this activity, it is not necessary to copy/paste the code that corresponds to your class. Rather, you can keep all the code for your class in Part A, and then just run the tests in the subsequent parts to verify that your code is working. Part A Start by creating a class called Recipe . Give this class an _init__() method that allows the user to set the title , ingredients , and directions as instance variables. That is, after having defined your class, you should be able to run the following code and receive the printed result. = cookies Recipe ("cookies, {"cookie jar : 1}, ["take a cookie out of the jar"]) print (cookies. title) print (cookies. ingredients) print (cookies. directions) cookies { cookie jar': 1} ['take a cookie out of the jar'] In [ ]: N # write class here In [ ]: # test code here Now add input checking. Modify the _init_() method to enforce the following conditions: 1. title must be a string. If not, raise an informative TypeError . 2. ingredients must be a dict. If not, raise an informative TypeError . 3. The keys of ingredients must all be strings. If not, raise an informative TypeError . == Hint: all ([x "cookies for x in container]) will check whether x has value "cookies" for all x in container . You can modify this idea to perform this check without writing a for - loop , although such a loop is also a fine approach. 4. The directions must be a list . If not, raise an informative TypeError . In this and future parts, you can modify your code in Part A -- no need to copy/paste your class. Write a simple test case for each of these four conditions to show that the corresponding error is raised. The first one is written for you. Each of these test cases can be completed in a single line. If you finish early, you can come back and add the following additional checks to your class: 1. The entries of directions must be strings. If not, raise an informative TypeError . 2. The values of ingredients must all be int s or float s. If not, raise an informative TypeError . 3. The values of ingredients must all be nonnegative. If not, raise an informative ValueError . In [ ]: # first test In [ ]: # second test In [ ]: # third test In [ ]: # fourth test Part D Implement attractive printing, such that, if cookies is a Recipe , then calling print (cookies) will print out the title, ingredients, and directions in an attractive and readable format. Feel free to be creative! Here's one illustration. Using the same recipe for cookies from Part C, print (cookies) # printed output How To Make Cookies Ingredients Flour (grams) : 400 Butter (grams) : 200 Salt (grams) : 10 Sugar (grams) : 100 Directions 1. In a large mixing bowl, cut the chilled butter into the flour and sugar. 2. Add the salt and sugar, and combine. 3. Roll into a log, and freeze. 4. Preheat the oven to 200 C. 5. Cut the log of dough into thin disks and place on baking sheet. 6. Bake for 12 minutes, flipping after 7 minutes. Hint printing is controlled by the _str_0 magic method. The str_0 method should return the string you desire to print. Actual printing should happen separately. Note: feel free to use any tools that you can think of to solve this problem. In [ ]: # demonstration of printing print (cookies)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Management Systems

Authors: Patricia Ward, George A Dafoulas

1st Edition

1844804526, 978-1844804528

More Books

Students also viewed these Databases questions

Question

What are the best practices for managing a large software project?

Answered: 1 week ago

Question

How does clustering in unsupervised learning help in data analysis?

Answered: 1 week ago