2013-10-07 40 views
0

所以,我试图跳过我的打印行并回到我的raw_input。我需要一个循环吗?Python-如何跳过返回时打印什么?

class stuff(Scene): 

    def enter(self): 
     print "This is text" 
     print "This is text" 
     print "This is text" 
     print "This is text" 
     print "This is text" 

     action = raw_input("> ") 

     if action == "blah" 
      print "This is text" 
      return 'stuff' 

当我做到这一点,重复我所有的打印行,我怎么得到它回去的raw_input?

+0

你最近怎么使用这个课程?目前还不清楚“我什么时候这样做”是指。 – geoffspear

+0

我正在使用如果为一个游戏,我已经做了我的引擎和地图,但我在课堂上遇到麻烦。如果这是有道理的。 –

+0

控制台输出通常不支持此操作。把控制台想象成打印机,在纸上打印日志。 – dansalmo

回答

1

您可以为班级创建一个属性,用于跟踪您之前是否打印过该文本。当你打印文本时,请适当设置属性。例如:

class stuff(Scene): 
    def __init__(self): 
     self.seen_description = False 
     #other initialization goes here 

    def enter(self): 
     print "End Of The Road" 
     if not self.seen_description: 
      print "You are standing beside a small brick building at the end of a road from the north." 
      print "A river flows south." 
      print "To the north is open country, and all around is dense forest." 
      self.seen_description = True 

     action = raw_input("> ") 

     if action == "go inside": 
      print "You enter the brick building" 
      return 'brick building' 

x = stuff() 
x.enter() 
x.enter() 

结果:

End Of The Road 
You are standing beside a small brick building at the end of a road from the north. 
A river flows south. 
To the north is open country, and all around is dense forest. 
> wait 
End Of The Road 
> wait 

在这里,我们得到了一个扩展的描述,我们第一次打电话enter,并跳过所有后续调用。

+0

这正是我期待的!非常感谢! –