2015-05-04 50 views
1

Python版本
蟒蛇-V 的Python 2.7.9 ::蟒蛇2.0.1(x86_64的)使用GETATTR()無返,以下錯誤:GETATTR():屬性名稱必須是字符串

沒有回報或退出它得到的錯誤:

line 16, in play room = getattr(self, next) TypeError: getattr(): attribute name must be string

ideone.com網上其確定

#-*-coding:utf-8-*- 
from sys import exit 


class newgame(object): 
    def __init__(self, start): 
     self.start = start 
     #self.cccl() 

    def play(self): 
     next = self.start 

     while True: 
      print "\n--------" 
      print next 
      room = getattr(self, next) 
      next = room() 

    def death(self): 
     print "this is death" 
     exit(-1) 


    def cccl(self): 
     print "this is bbbb" 
     #return self.al() 
     #exit(1) 

    def al(self): 
     print "this is al" 
     action = raw_input("> ") 
     if action == '1': 
       return "death" 
     elif action == '2': 
       return "cccl" 


ngame = newgame("al") 
ngame.play() 

回答

2

您在while循環中設置了next = room(),它在第一次迭代後設置爲None旁邊,如果您想要轉到al的方法,則需要返回self.play。

我想你也想所謂的死亡和CCCL:

def al(self): 
     print "this is al" 
     action = raw_input("> ") 
     if action == '1': 
       return self.death() 
     elif action == '2': 
       return self.cccl() 
     return self.play() 

我會避免使用next作爲陰影內建函數的變量名。

+0

哦,我知道了,謝謝 而Ture: – poone

相關問題