1

I'm a beginner in python and I'm having a problem in this program:

Here's the NodeList first:

class Node:
    def __init__(self,initdata):
        self.data = initdata
        self.next = None

    def getData(self):
        return self.data

    def getNext(self):
        return self.next

    def setData(self,newdata):
        self.data = newdata

    def setNext(self,newnext):
        self.next = newnext

My problem is in this program: (The print)

from NodeList import Node

class StackLL:

    def __init__(self):
        self.head = None

    def pushSLL(self,item):
        temp = Node(str(item))
        temp.setNext(self.head)
        self.head = temp
        node = self.head
        print(chr(0x2510)+(chr(0x0000)*(len(item)))+chr(0x250c))
        while node!=None:
            stackeditem = chr(0x2502)+node.data+chr(0x2502)
            print(stackeditem)
            node = node.next   
        print(chr(0x2514)+(chr(0x2500)*(len(item)-1))+chr(0x2518))

here's the output

Everytime I print, the lines just seems off. I tried to experiment using len() just to make it accurate, but everytime the item adds more characters it gets off again. Any help would be greatly appreciated. Thank you.

1
  • From your picture it seems that whatever GUI you have uses a proportional font. You will have a hard time adjusting the width of a string by adding spaces. Use a monospaced font. Commented Oct 7, 2013 at 3:35

2 Answers 2

4

Use a monospace font in IDLE settings and you are all good.

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

Comments

3

Use a monospace font as @Kable answered.

In addition to that, the last print print 1 less chr(0x2500).

Some other notes:

  • Use \u2502 instead of chr(0x2502).
  • Separate dump functionality from pushSLL.
  • The first and the last print only consider the length of the current node. It should calculate maximum length for all nodes.

class StackLL:

    def __init__(self):
        self.head = None

    def pushSLL(self, item):
        temp = Node(str(item))
        temp.setNext(self.head)
        self.head = temp

    def dump(self):
        length = max(len(node.data) for node in self.allNodes()) if self.head else 0
        print('\u2510{}\u250c'.format(' '*length))
        for node in self.allNodes():
            print('\u2502{:<{}}\u2502'.format(node.data, length))
        print('\u2514{}\u2518'.format('\u2500'*length))

    def allNodes(self):
        node = self.head
        while node is not None:
            yield node
            node = node.next

s = StackLL()
s.pushSLL('Arc')
s.pushSLL('Circle')
s.pushSLL('Rect')
s.dump()

output

Comments

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.