0

所以,我试图跳过我的打印行并回到我的 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?

4

1 回答 1

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,并且在所有后续调用中都会跳过它。

于 2013-10-07T18:32:00.480 回答