[Tutor] Using Class Properly - early beginner question

Rafael Knuth rafael.knuth at gmail.com
Wed Mar 22 08:30:42 EDT 2017


thanks for your feedback! @boB

I wrote a function that does exactly what I want, and that is:
Create a shopping list and then let the user decide which items (food)
are supposed to be instantly consumed and which ones stored.

def ManageFood():
    create_shopping_list = []
    prompt = ("Which foods would you like to purchase?\nEnter 'quit' to exit. ")
    food = input(prompt)

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

    print("These are your foods on your shopping list: %s." % ", "
.join(create_shopping_list))
    eat_food = []
    store_food = []
    for food in create_shopping_list:
        print("You bought this item: %s. " % (food))
        prompt = input("What would you like to do with it?\nEnter
'eat' or 'store'. ")
        if prompt == "eat":
            eat_food.append(food)
        elif prompt == "store":
            store_food.append(food)
    print("Food you want to eat now: %s." % ", " .join(eat_food))
    print("Food you want to store: %s." % ", " .join(store_food))

ManageFood()

PS: Please let me know if you have any suggestions how to write my
code above in a shorter, more elegant fashion (it does what it's
supposed to do, but not sure if a pro would write it same way I did).

Besides that, I want to take it a step further and rewrite the
function above as a class, and I don't know how exactly how to do
this.
(coding newbie pains ... I just learned the basics about classes in
Python, but nowhere could I find examples of how to properly
initialize classes, given that it operates solely with user input -
same goes with with calling that class properly). Here's how far I got
on my own:

class FoodShopping(object):
    def __init__ (self, create_shoppping_list, prompt, food, eat_food,
store_food):
        self.create_shopping_list = create_shopping_list
        self.prompt = prompt
        self.food = food
        self.eat_food = eat_food
        self.store_food = store_food

    def ManageFood(self, create_shopping_list, prompt, food, eat_food,
store_food):
        create_shopping_list = []
        prompt = ("Which foods would you like to purchase?\nEnter
'quit' to exit. ")
        food = input(prompt)

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

        print("These are your foods on your shopping list: %s." % ", "
.join(create_shopping_list))
        eat_food = []
        store_food = []
        for food in create_shopping_list:
            print("You bought this item: %s. " % (food))
            prompt = input("What would you like to do with it?\nEnter
'eat' or 'store'. ")
            if prompt == "eat":
                eat_food.append(food)
            elif prompt == "store":
                store_food.append(food)
        print("Food you want to eat now: %s." % (eat_food))
        print("Food you want to store: %s." % (store_food))

FoodShopping()

That's the error message I get when executing my code:

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_29.py ==
Traceback (most recent call last):
  File "C:/Users/Rafael/Documents/01 - BIZ/PYTHON/Python
Code/PPC_29.py", line 140, in <module>
    FoodShopping()
TypeError: __init__() missing 5 required positional arguments:
'create_shoppping_list', 'prompt', 'food', 'eat_food', and
'store_food'
>>>


More information about the Tutor mailing list