[Tutor] Can anyone help answer this?

DL Neil PyTutor at danceswithmice.info
Wed Jun 10 04:09:20 EDT 2020


On 10/06/20 2:29 PM, Christine Mason wrote:
> Given the following class definition:
> 
>   class Food:
> 
>       def __init__(self, name, taste):
> 
>          self.name
> <https://aus01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fself.name%2F&data=02%7C01%7Cchristine.massof%40ird.govt.nz%7C12c2d3472aba4bbc306f08d80ce1e902%7Cfb39e3e923a9404e93a2b42a87d94f35%7C1%7C0%7C637273511711638197&sdata=cM05RAGTtzn%2B1%2BDwU3c6UXadLKFLgyAR5iL2xJGte4g%3D&reserved=0>
> = name
> 
>          self.taste = taste
> 
>   Write a function *createFood()* that takes a list of food items as
> argument and creates an instance of class Food for each of them.  It then
> returns the list of instances that it has created.
> 
>   Each food item in the list that is passed as argument to *createFood() *is
> a tuple of the form ('name', 'taste'), so the list of food items might look
> like this:
> 
>   [('curry', 'spicy'), ('pavlova', 'sweet'), ('chips', 'salty')]
> 
>   The function *createFood() *takes the two elements of each tuple and
> passes them to the initialiser of Food.  It then collects the objects
> returned by the initialiser and adds them to the list that is returned by
> createFood().
> 
> DO NOT call the function


Is this an homework assignment? If so, for which course - and if you can 
give us on-line references, that's a bonus!

Assuming the above, we will be happy to *help* *you* find a solution. 
What code do you have so far? What results? Please copy-paste them into 
your reply.

For example:
- how do you instantiate an example of the Food class?
- how will you loop through each of the foods in the input list?tuple?
- how will you create, and then add, results to the output list?

The last injunction is badly-expressed. I presume that you are to 
forward your work to be graded as a function - and only a function (a 
common methodology to ease the grading process). However, to test your 
code, as a function, you will need to call it from somewhere! Either add 
a temporary function-call for testing (and remember to remove it before 
handing-in), or have you already learned about TDD (Test-Driven 
Development), eg pytest, unittest, ...?
-- 
Regards =dn


More information about the Tutor mailing list