2012-04-01 13 views
0

何とか改行が正常に機能していない。改行が機能しない - Python

Expected: 
    O meu u2 2 post 
    http://www.yahoo.com 
    1 Gosto, 0 Nao gosto 
    <BLANKLINE> 
    O meu u2 post 
    http://www.altavista.com 
    1 Gosto, 0 Nao gosto 
Got: 
    'O meu u2 2 post\nhttp://www.yahoo.com\n1 Gosto, 0 Nao Gosto\n\nO meu u2\nhttp://www.yahoo.com\n1 Gosto, 0 Nao Gosto' 

これは、関数で使用されるコードです: これは私が得るものです。 重要な部分は、あなたが\nとしてあなたに改行文字が表示される文字列の表現を見ているように見えます

class Comments(): 
def __init__(self, u=None, text='', link=None): 
    self.u = u 
    self.text = text 
    self.link = link 
    self.topo = None 
    self.fim = None 

def __str__(self): 
    actual = self.topo 
    s = '' 
    if actual == None: 
     return None 
    while actual != None: 
     if actual.seg == None: 
      s += str(actual) 
      actual = actual.seg 
     else: 
      s += str(actual) + '\n' + '\n' 
      actual = actual.seg 
    return s 

def add(self,comment): 
    if self.topo == None: 
     self.topo = comment 
     self.fim = comment 
    else: 
     comment.seg = self.topo 
     self.topo.ant = comment 
     self.topo = comment 

def remove(self,comment): 
    actual = self.topo 
    if (self.topo == self.fim) and (self.topo == comment): 
     self.topo = None 
     self.fim = None 
    while actual!=None: 
     if actual == comment: 
      if self.topo==comment: 
       actual.seg.ant = None 
       self.topo = actual.seg 
      elif self.fim==comment: 
       actual.ant.seg = None 
       self.fim = actual.ant 
      else: 
       actual.seg.ant = actual.ant 
       actual.ant.seg = actual.seg 
      break 
     else: 
      actual = actual.seg 

def countLike(self): 
    count = 0 
    actual = self.topo 
    while actual != None: 
     if len(actual.likeList) >= 1: 
      count += 1 
      actual = actual.seg 
     else: 
      actual = actual.seg 
    return count 

def showRecentComments(self,n): 
    count = 1 
    actual = self.topo 
    sC = '' 
    if actual == None: 
     return None 
    while actual != None: 
     if count < n: 
      sC += str(actual) + '\n' + '\n' 
      count += 1 
      actual = actual.seg 
     elif count == n: 
      sC += str(actual) 
      count += 1 
      actual = actual.seg 
     elif count > n: 
      break 
    return sC 

よろしく、ネルソン・グレゴリオ

+0

"これは私が得るものです:" ..まあ、あなたは何をしていますか? –

+0

'__str__'から' None'を返すのではなく、 ''''を返すべきです。 –

答えて

2

STRとshowRecentComments機能する必要があります。 printか、たとえばに書いてください。 stdout(sys.stdout.write(s))文字列ではなく、改行が展開されます。

+0

しかし、__str__関数は機能します。 showRecentCommentsも同じにする必要がありますか? ----- 実際に返品の代わりに印刷が行われます。 私はそれがすると思います。 –

+0

'return'はまだ意味がありますが、返される文字列を' print'したいとします。例えばあなたの 'Comment'クラスのインスタンスとして' c'を持っていたら、 'print c'を実行して、' __str__'を使ってそのインスタンスの文字列表現を取得し、それを印刷することができます。 – zigg