Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I finished part A, but for part B, I don't know how to do it. Can you help me to figure it out? Thank you
I finished part A, but for part B, I don't know how to do it. Can you help me to figure it out? Thank you so much
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. 1}, ["take a cookie out of the jar"]). cookies = Recipe("cookies", {"cookie jar" print(cookies.title) print(cookies. ingredients) print(cookies.directions) cookies {'cookie jar': 1} ['take a cookie out of the jar'] # write class here class Recipe: def __init__(self, title, ingredients, directions): self.title=title self.ingredients=ingredients self.directions=directions Part B 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. # first test # second testStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started