Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(c) Let's define a Dish (that might be served at a restaurant) with three fields: a string for the name of the dish, a number

(c) Let's define a Dish (that might be served at a restaurant) with three fields: a string for the name of the dish, a number for its price, and a number for the number of calories in the dish. As you write the functions in this problem (and every problem), it's essential that you follow the design recipe (Links to an external site.)Links to an external site., especially specifying the types of the parameters and return values. You'll need to distinguish between functions that take single items and functions that take lists of items, for example; you'll run into trouble if you're not clear on that to start with. Writing examples (in the form of assert statements) is similarly essential.

(c.1) Define a namedtuple for representing dishes like this and create three actual Dish objects (just make up the values for each Dish and assign them to variables named dish1, dish2, and dish3).

(c.2) Write a function called dish_str that takes a Dish and returns a string in this form: Paht Woon Sen ($9.50): 330 cal (You don't have to format the dollar amount perfectly at this point.)

(c.3) Write a function called dish_same that takes two dishes as arguments and returns True if the names of the two dishes and their calorie counts are equal (and False otherwise). Write some tests using assert statements (perhaps including dish1, dish2, and dish3); they should include calls with two identical dishes, two dishes that are the same except for their price, and two dishes that differ in their names, calorie counts, or both. Of course your tests should be included in your lab5.py file, and of course you should have tests like this for every function you write, except maybe the ones that print instead of returning a value. Coming up with thorough tests is another programming skill. Some people enjoy trying to "break" software; they become software quality assurance (testing) experts.

(c.4) Write a function called dish_change_price that takes a Dish and a number and returns a Dish that's the same as the parameter except that its price is changed as follows: The number (positive or negative) represents a percentage change in price (so that 100 would double the price and 50 would cut it in half). (This may require you to think a little about the arithmetic you need to compute this result. Figure it out before you write any code; come up with a half-dozen different examples and their results.)

(c.5) Write a function called dish_is_cheap that takes a Dish and a number and returns True if the Dish's price is less than that number (and False otherwise).

(c.6) Now create a list called dish_list_1 of at least five Dish objects. Play around with this list in the shell for a minute or two (take its length, sort it, append another Dish to the end) to make sure it works as you expect. Next create another list called dish_list_2 that contains at least four dish objects. Then create one big list by extending dish_list_1 with dish_list_2. (Note the difference between the append() method and the extend() method; take a minute to be sure.) Write a function called dishlist_display that takes a list of Dishes and returns one large string consisting of the string representation of each dish followed by a newline (' ') character. We've done something similar to this with a collection of restaurants. Write some tests, as usual. Then use a print statement to print the string representation of all the dishes in the big list you created above.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions