[Tutor] Using Class Properly - early beginner question

boB Stepp robertvstepp at gmail.com
Wed Mar 22 22:49:09 EDT 2017

On Wed, Mar 22, 2017 at 7:30 AM, Rafael Knuth <rafael.knuth at gmail.com> wrote:
> thanks for your feedback! @boB

> ...(it does what it's
> supposed to do, but not sure if a pro would write it same way I did).

I'll leave it to others to evaluate your function which I snipped as I
am not a pro! ~(:>))

> 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.

I'm not sure if you read the entirety of my answer to your previous
post.  I tried to point out that you had some issues with how you were
passing in arguments, and then not doing anything constructive with
them.  You might want to look that over again.

For now I will give you something that will generate a shopping list
without passing in any arguments, which seems to be mostly what you
are trying to do.  I am not trying to duplicate your work exactly, but
here goes what I have for you:


#!/usr/bin/env python3

class GroceryListMaker:  # Explicit inheritance from "object" is
optional in Py 3.
    def __init__(self):
        self.shopping_list = []
        self.prompt = ('What food items would you like to buy?\n(Type "quit"'
                ' to exit):  ')

    def make_shopping_list(self):
        while True:
            food = input(self.prompt)
            if food == 'quit':

    def display_shopping_list(self):
        print('\n\nYour shopping list now has the following items:\n')
        for item_number, item in enumerate(self.shopping_list):
            print('%s.  %s' % (item_number, item))

if __name__ == '__main__':
    my_shopping_list = GroceryListMaker()


I won't claim that this is the greatest piece of code as I am a
learner like yourself.  But when I run the above I get:


> python -i buy_food.py
What food items would you like to buy?
(Type "quit" to exit):  spam
What food items would you like to buy?
(Type "quit" to exit):  limberger cheese
What food items would you like to buy?
(Type "quit" to exit):  rotten eggs
What food items would you like to buy?
(Type "quit" to exit):  green ham
What food items would you like to buy?
(Type "quit" to exit):  quit

Your shopping list now has the following items:

0.  spam
1.  limberger cheese
2.  rotten eggs
3.  green ham


See if the above makes things any clearer.

As for your efforts below, you are again failing to pass in the
necessary arguments that you specify in __init__().  That is what your
error traceback is trying to tell you.  You had the same issue last
time.  What I am talking about I'll try to illustrate with a simple


Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900
64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
py3: class TextRepeater:
...     def __init__(self, text_to_repeat):
...             self.text_to_repeat = text_to_repeat
...     def echo_text(self):
...             print(self.text_to_repeat)
py3: text_repeater = TextRepeater('Is "Monty Python and the Holy
Grail" great or what?')
py3: text_repeater.echo_text()
Is "Monty Python and the Holy Grail" great or what?


Does this make object initialization with a passed in argument any clearer?

> (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