1

I've searched around and found similar questions, but cannot seem to figure out how to apply it to my issue. I'm creating a shape with a predefined size, but first I need to figure out how to add/multiply the "-" character by a certain number and print it within classes. Here's my code:

def __str__(self):

    string = "-" * 4
    return string

def main():

    print(str.string)
    return
main()

end goal is to create a box like this: enter image description here

1

1 Answer 1

1

Let me tell you one thing! creating function in the name of keywords is not preferred as the compiler may not behave expected! So calling str.Variable_name creates problem

Just make your code like this :

def get_box():
    return '-'*3

print(get_box())

This should work! Let me know if not -

Update

Class Box(object):
    def __init__(self):
        self.box = "-"

    def __str__(self):
        return self.box*3

#create an object
box = Box()
print(box)

Check and let me know whether this works...

Sign up to request clarification or add additional context in comments.

4 Comments

print(get_box()) You forgot the ().
I would do that, however I have to use the class str for the string portion of this project, as we are learning about classes in my programming class. He wrote out a bunch of pre-made classes and we have to fill in the missing portions.
You didnt mention that it is inside a class! Fine, now i have updated my answer accordingly, do check and let me Know if it helps! @JohnGordon thanks for finding it out! i edited!
Glad that it helped :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.