0

I don't really know what I'm doing, the only successful classes I've written have been in Java. I can't seem to get this code to work. It keeps giving me 'small_box is not defined' along with 'stars_stripes = StarsAndStripes()' and no explanation on the second traceback.

    class StarsAndStripes():

    def __init__(self):
        self.small_box = small_box()
        self.big_box = big_box()

    def twenty_stars():
        for i in range(20):
            print("*")
        print("\n")

    def twenty_dashes():
        for i in range(20):
            print("-")
        print("\n")

    def small_box():
        for i in range(3):
            print(twenty_dashes)
            print(twenty_stars)
        print(twenty_dashes)

    def big_box():
        print(small_box)
        print(small_box)

stars_stripes = StarsAndStripes()
print(stars_stripes.small_box)
print("\n\n")
print(stars_stripes.big_box)

The result should look something like this:

--------------------
********************
--------------------
********************
--------------------
********************
--------------------


--------------------
********************
--------------------
********************
--------------------
********************
--------------------
--------------------
********************
--------------------
********************
--------------------
********************
--------------------
Paul Duft
  • 13
  • 1
  • All those variables are not defined anywhere. It looks like you need to read up on the basics of Python class definitions. Note, you never use any internal state here, so I likely wouldn't write this as a class to begin with, just module level functions. – juanpa.arrivillaga Oct 11 '18 at 21:21
  • Hints: A) you need to do self.small_box() to call small_box from another function in the class. B) You need to put self in all of your functions for the class C) You need to call the function with ()'s – Foon Oct 11 '18 at 21:21
  • You can't have instance attributes with the same name as instance methods. Both are accessed as `self.small_box`; one will end up overwriting the other. – Aran-Fey Oct 11 '18 at 21:22

2 Answers2

2

You forgot to reference the instance of the class. This is done using self in python, as first parameter to your methods.

Also, you don't have to print the result of you method calls, since they actually already print to the output (I've changed the body of your methods).

And you don't need to call you class's methods within your __init__ function.

class StarsAndStripes():

    def __init__(self):
        pass

    def twenty_stars(self):
        for i in range(20):
            print("*")
        print("\n")

    def twenty_dashes(self):
        for i in range(20):
            print("-")
        print("\n")

    def small_box(self):
        for i in range(3):
            self.twenty_dashes()
            self.twenty_stars()
        self.twenty_dashes()

    def big_box():
        self.small_box()
        self.small_box()

stars_stripes = StarsAndStripes()
stars_stripes.small_box()
print("\n\n")
stars_stripes.big_box()
Léopold Houdin
  • 1,515
  • 13
  • 18
  • Also, if you did want to use your class methods as properties for some reason, you could do it through the `@property` decorator. – prithajnath Oct 11 '18 at 21:42
-1

I think the problem is in your __init__ method. You are trying to call a small_box (non-class-member) function, but it doesn't exist. Try removing the entire __init__ method, I don't think you need it.

dshepherd
  • 4,989
  • 4
  • 39
  • 46