Python: Print using ascii codes -
i'm beginner in python , i'm having problem in program:
here's 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 in 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))
everytime print, lines seems off. tried experiment using len() make accurate, everytime item adds more characters gets off again. appreciated. thank you.
use monospace font @kable answered.
in addition that, last print
print 1 less chr(0x2500)
.
some other notes:
- use
\u2502
instead ofchr(0x2502)
. - separate dump functionality
pushsll
. - the first , last print consider length of current node. should calculate maximum length 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) node in self.allnodes()) if self.head else 0 print('\u2510{}\u250c'.format(' '*length)) 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 not none: yield node node = node.next s = stackll() s.pushsll('arc') s.pushsll('circle') s.pushsll('rect') s.dump()
Comments
Post a Comment