[Tutor] Using Class Properly - early beginner question

Rafael Knuth rafael.knuth at gmail.com
Tue Mar 21 13:20:21 EDT 2017


I am trying to write a food shopping list.
The user should be able to add items to that shopping list, and later
on decide what should happen to those purchased foods: instantly
consumed or stored.

My initial idea was to create a parent class to populate the shopping
list and a child class to manage the purchased items as described
above.

While writing the parent class, I ran into the following issue:
How do I properly declare a variable that takes user input?
Do I write methods in the same fashion like in a regular function?
And how do I call that class properly?

This is what I came up with:

class BuyFoods(object):
    def __init__(self, outlet):
        self.outlet = outlet
    def CreateShoppingList(self, shopping_list, prompt, food):
        self.shopping_list = shopping_list
        self.prompt = prompt
        self.food = food
        shopping_list = []
        prompt = ("Which foods would you like to purchase?\nEnter
'quit' to exit. ")
        food = input(prompt)

        while food != "quit":
            shopping_list.append(food)
            food = input(prompt)

        print("You just purchased these foods: %s." % ", ".join(shopping_list))

Tesco = BuyFoods("Tesco")
Tesco.CreateShoppingList()

That's the error message I get:

Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900
32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
== RESTART: C:\Users\Rafael\Documents\01 - BIZ\PYTHON\Python Code\PPC_28.py ==
Traceback (most recent call last):
  File "C:\Users\Rafael\Documents\01 - BIZ\PYTHON\Python
Code\PPC_28.py", line 136, in <module>
    Tesco.CreateShoppingList()
TypeError: CreateShoppingList() missing 3 required positional
arguments: 'shopping_list', 'prompt', and 'food'
>>>


More information about the Tutor mailing list