Python-如何跳过返回时打印的内容?

时间:2013-10-07 18:06:14

标签: python python-2.7

所以,我试图跳过我的印刷线并回到我的raw_input。我需要一个循环吗?

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?

1 个答案:

答案 0 :(得分: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时会得到一个扩展描述,并且会在所有后续调用中跳过它。