首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python-返回时如何跳过打印的内容?

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

Stack Overflow用户
提问于 2013-10-07 18:06:14
回答 1查看 59关注 0票数 0

因此,我试图跳过我的打印行,回到我的raw_input。我需要一个循环吗?

代码语言:javascript
复制
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?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-07 18:32:00

您可以为类创建一个属性,以跟踪您以前是否打印过文本。打印文本时,适当地设置属性。例如:

代码语言:javascript
复制
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()

结果:

代码语言:javascript
复制
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时得到了一个扩展描述,并且在所有后续调用中都跳过了它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19231610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档