首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >同时循环打印变量值一次迭代延迟。

同时循环打印变量值一次迭代延迟。
EN

Stack Overflow用户
提问于 2019-07-22 18:06:34
回答 2查看 77关注 0票数 0

我正在学习编程,用艰苦的方法学习Python。在练习43的学习训练中,作者希望我们为英雄遇到敌人建立一个简单的战斗系统。

在我的战斗系统中,玩家和敌人各从100点开始。我设置了一个包含4个提示的列表,玩家将被随机呈现。基于提示符,玩家需要用一个动作来回应。根据场景和反应,玩家会失去HP,对敌人造成伤害,或者不受影响。当玩家和敌人的剩余HP发生变化时,程序会同时打印出来。

代码语言:javascript
复制
health = 100
enemy_health = 100

battle_prompts = [
    "The Gothon raises his rifle, aiming it square in your face.",
    "The Gothon aims, then pauses, realising he's yet to reload his ammo.",
    "The Gothon charges at you at lightspeed, fists clenched tight.",
    "The Gothon runs in your direction, with a menacing grin on his face."
]

while health != 0 and enemy_health != 0:
    battle_prompt = battle_prompts[randint(0,3)]
    print (battle_prompt)
    action = input("> ")
    healthbar = (f"HEALTH: {health} | ENEMY HEALTH: {enemy_health}")
    if battle_prompt == battle_prompts[0] and action == "dodge":
        print ("You dodge the bullet successfully. Nice one!")
    elif battle_prompt == battle_prompts[0] and action != "dodge":
        health -= 25
        print ("You fool! You just got shot!")
        print (healthbar)
    elif battle_prompt == battle_prompts[1] and action == "shoot":
        enemy_health -= 25
        print ("Cool! You just caused some alien bloodshed!")
        print (healthbar)
    elif battle_prompt == battle_prompts[1] and action != "shoot":
        print ("The Gothon reloads his rifle successfully. A wasted opportunity!")
    elif battle_prompt == battle_prompts[2] and action == "block":
        print("You manage to block the Gothon's deadly punches. Way to go!")
    elif battle_prompt == battle_prompts[2] and action != "block":
        health -= 25
        print("What a lamebrain! You just got pounded by an alien!")
        print (healthbar)
    elif battle_prompt == battle_prompts[3] and action == "punch":
        enemy_health -= 25
        print("You give that extraterrestrial invader a beautiful uppercut. Wow!")
        print (healthbar)
    elif battle_prompt == battle_prompts[3] and action != "punch":
        print("The Gothon knocks you over. You're not hurt, but you wasted an opportunity.")
else:
    if health == 0:
        print ("Whoops! Guess you're outta health points! That's all folks!")
        return 'death'
    elif enemy_health == 0:
        print("Good job! You defeated an armed citizen of a galaxy unknown fair and square!")
        return 'escape_pod'

我将剩下的HP提示符分配给一个变量healthbar,但似乎无法使它正常工作。如果HP有更改,则不会在后面立即打印的提示符中反映出来;只有在下一次惠普发生更改时才会显示。我想你可以说提示总是一个循环迭代‘迟到’。

例如:如果我造成25点伤害,第一轮输出是HEALTH: 100 | ENEMY HEALTH: 100,在第二轮,敌人造成25点伤害。输出:HEALTH: 100 | ENEMY HEALTH: 75如果我躲避攻击,第三轮输出是:HEALTH: 75 | ENEMY HEALTH: 75

该程序在“实时”中计算HP值,即,如果任何字符的HP命中为零,则执行while-循环的else块,但提示仍然显示它们还剩25 HP。

我尝试将healthbar移到while-循环之外,但是提示显示HP值总是在100。但是,当我完全取消了healthbar变量,并通过粘贴整个提示符来替换它的每个实例时,一切都会正确显示。我做错了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-22 18:12:33

您正在用旧的健康值格式化healthbar (创建字符串),修改健康值,然后再次打印旧的健康值。

我建议制作一个“可格式化”的字符串模板,并且只在必要时进行格式化。您还可以创建一个函数来打印它。我相信您知道如何为此编写函数,下面是如何使用格式模板技术:

代码语言:javascript
复制
health = 100
enemy_health = 100

battle_prompts = [
    "The Gothon raises his rifle, aiming it square in your face.",
    "The Gothon aims, then pauses, realising he's yet to reload his ammo.",
    "The Gothon charges at you at lightspeed, fists clenched tight.",
    "The Gothon runs in your direction, with a menacing grin on his face."
]

# Template
healthbar = "HEALTH: {health} | ENEMY HEALTH: {enemy_health}"

while health != 0 and enemy_health != 0:
    battle_prompt = battle_prompts[randint(0,3)]
    print (battle_prompt)
    action = input("> ")
    if battle_prompt == battle_prompts[0] and action == "dodge":
        print ("You dodge the bullet successfully. Nice one!")
    elif battle_prompt == battle_prompts[0] and action != "dodge":
        health -= 25
        print ("You fool! You just got shot!")
        print (healthbar.format(health=health, enemy_health=enemy_health))
    elif battle_prompt == battle_prompts[1] and action == "shoot":
        enemy_health -= 25
        print ("Cool! You just caused some alien bloodshed!")
        print (healthbar.format(health=health, enemy_health=enemy_health))
    elif battle_prompt == battle_prompts[1] and action != "shoot":
        print ("The Gothon reloads his rifle successfully. A wasted opportunity!")
    elif battle_prompt == battle_prompts[2] and action == "block":
        print("You manage to block the Gothon's deadly punches. Way to go!")
    elif battle_prompt == battle_prompts[2] and action != "block":
        health -= 25
        print("What a lamebrain! You just got pounded by an alien!")
        print (healthbar.format(health=health, enemy_health=enemy_health))
    elif battle_prompt == battle_prompts[3] and action == "punch":
        enemy_health -= 25
        print("You give that extraterrestrial invader a beautiful uppercut. Wow!")
        print (healthbar.format(health=health, enemy_health=enemy_health))
    elif battle_prompt == battle_prompts[3] and action != "punch":
        print("The Gothon knocks you over. You're not hurt, but you wasted an opportunity.")
else:
    if health == 0:
        print ("Whoops! Guess you're outta health points! That's all folks!")
        return 'death'
    elif enemy_health == 0:
        print("Good job! You defeated an armed citizen of a galaxy unknown fair and square!")
        return 'escape_pod'
票数 3
EN

Stack Overflow用户

发布于 2019-07-22 18:12:52

在每次操作之后,您将打印旧的 healthbar。然后,您进入循环的底部,输入下一个操作,然后使用前面操作的结果更新healthbar

通过将打印与文本分离来解决这个问题。更新并打印在循环的底部:

而health != 0和enemy_health != 0: battle_prompt = battle_promptsrandint(0,3) print (battle_prompt) action = and (“> ")

代码语言:javascript
复制
if battle_prompt == battle_prompts[0] and action == "dodge":
    print ("You dodge the bullet successfully. Nice one!")
elif battle_prompt == battle_prompts[0] and action != "dodge":
    health -= 25
    print ("You fool! You just got shot!")
... all other choices ...

healthbar = (f"HEALTH: {health} | ENEMY HEALTH: {enemy_health}")
print (healthbar)

输出:

代码语言:javascript
复制
The Gothon runs in your direction, with a menacing grin on his face.
> shoot
The Gothon knocks you over. You're not hurt, but you wasted an opportunity.
HEALTH: 100 | ENEMY HEALTH: 100
The Gothon runs in your direction, with a menacing grin on his face.
> punch
You give that extraterrestrial invader a beautiful uppercut. Wow!
HEALTH: 100 | ENEMY HEALTH: 75
The Gothon charges at you at lightspeed, fists clenched tight.
> punch
What a lamebrain! You just got pounded by an alien!
HEALTH: 75 | ENEMY HEALTH: 75
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57151532

复制
相关文章

相似问题

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