【问题标题】:How do you modify the value of a global variable, and have the global variable retain that new value?如何修改全局变量的值,并让全局变量保留该新值?
【发布时间】:2017-01-31 20:06:53
【问题描述】:

总的来说,我是编码新手,我选择 Python 作为我学习的第一语言,因为一些朋友说它简单有趣。我决定制作一个基于文本的小游戏,我的演示很顺利,除了一个问题。我无法对健康状况进行调整,经过大量测试和搜索后,我仍然无法弄清楚如何进行这项工作。

def combat():
    global playerhealthpoints
    playerhealthpoints = 100
    def cavecombat():
        combatroundinput = raw_input("The round has begun. Do you Block, Dodge, or Run?").lower()
        if combatroundinput == "block":
            enemyattackroll = random.randrange(1,10)
            if enemyattackroll < 5:
                print "Enemy has missed their attack this round."
                cavecombat()
            elif enemyattackroll >= 5:
                enemydamageroll = random.randrange(1,10) / 2
                playerhealthpoints = playerhealthpoints - enemydamageroll
                print "You have taken %s damage." %enemydamageroll
                print "You have %s health remaining." %playerhealthpoints
                cavecombat()
        elif combatroundinput == "dodge":
            enemyattackroll = random.randrange(1,10) / 2
            if enemyattackroll < 5:
                print "Enemy has missed their attack this round."
                cavecombat()
            elif enemyattackroll >= 5:
                enemydamageroll = random.randrange(1,10)
                playerhealthpoints = playerhealthpoints - enemydamageroll
                print "You have taken %s damage." %enemydamageroll
                print "You have %s health remaining." %playerhealthpoints
                cavecombat()
        elif combatroundinput == "run":
            playerrunroll = random.randrange(1,10)
            enemyattackroll = random.randrange(1,10) * 2
            if enemyattackroll < 5 and playerrunroll <5:
                print "Enemy has missed their attack this round."
                print "You have failed to run from this encounter."
                cavecombat()
            elif enemyattackroll >= 5 and playerrunroll >= 5:
                enemydamageroll = random.randrange(1,10)
                playerhealthpoints = playerhealthpoints - enemydamageroll
                print "You have taken %s damage." %enemydamageroll
                print "You have %s health remaining." %playerhealthpoints
                print "You have succeeded in escaping the enemy."

            elif enemyattackroll < 5 and playerrunroll >= 5:
                print "Enemy has missed their attack this round."
                print "You have succeeded in escaping the enemy."

            elif enemyattackroll >= 5 and playerrunroll < 5:
                 enemydamageroll = random.randrange(1,10)
                 playerhealthpoints = playerhealthpoints - enemydamageroll
                 print "You have taken %s damage." %enemydamageroll
                 print "You have %s health remaining." %playerhealthpoints
                 print "You have failed to run from this encounter."
                 cavecombat()
        else:
            print "Learn to type, you suck."
            cavecombat()
    cavecombat()
combat()

这是我最近一次坚持健康的尝试,欢迎提出任何建议。

【问题讨论】:

  • 有很多代码需要我们弄清楚!尝试将问题简化为几行并将其发布...以及到底出了什么问题。
  • 在函数外声明变量。然后使用 global 从函数内部引用它。也就是说 - 我不建议使用全局变量。

标签: python python-2.7 global-variables


【解决方案1】:

您需要return 健康。我也是python的新手,所以最好检查一下。函数里面的IMO函数很奇怪。尝试将函数分开,使用return保留值。

只是为了说明我的意思。

以下将return 一个pl 值:

def cavecombat():
    combatroundinput = input("The round has begun. Do you Block, Dodge, or Run?").lower()
    if combatroundinput == "block":
        enemyattackroll = random.randint(1, 10)
        if enemyattackroll < 5:
            print("Enemy has missed their attack this round.")
            pl = 0
            return pl
        elif enemyattackroll >= 5:
            enemydamageroll = random.randint(1, 10) / 2
            pl = enemydamageroll
            print("You have taken %s damage." % enemydamageroll)
            return pl

现在可以进行以下操作:

伤害 = 洞穴战斗()

它会执行函数,然后将pl的值破坏。

所以如果上面的印刷品说“你受到了 3 点伤害。 伤害值为 3。

【讨论】:

  • 是的,这比使用全局变量要好。让combat 返回一个值,然后使用playerhealthpoints = combat() 获取函数外部的值。
  • @JohnPfeifer 我最终可能会给出错误的建议。在上面添加一些代码来说明我的意思。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-04
  • 1970-01-01
  • 1970-01-01
  • 2011-11-18
  • 2021-06-10
相关资源
最近更新 更多