python如何继续循环回初始值?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (208)

我试着做一个简单的小游戏,在玩家和电脑之间的搏斗中,我用随机的值给出一个随机的值。健康值不断地循环回到原来的值,我似乎不知道为什么.我已经找了好几天了。

from random import randint

n = input()
print ('Welcome, ', n, ' Choose your weapon: Sword, Axe, or Mace.')
b = input()

if b == {'Sword'}:
    print ('Good Choice ', (n), ' the Sword will keep you safe.')
elif b == {'Axe'}:
    print ('Good Choice ', (n), ' the Axe will do some damage.')
elif b == {'Mace'}:
    print ('Probably a bad choice ', (n), ' the mace is heavy and slow but may protect you.')

print ('The Knight Approaches! ', (n), ', Get your', (b),' and get ready!')


player = 50 
knight = 50

battle = True

while player >= 0  or knight >= 0:

    if b == 'Sword':
        k2 = (knight - randint(3,4)) 
        p2 = (player - randint(2,6))
        print ('You have struck the knight!  His health is now', k2)
        print ('He has hit you as well!  Your health is now', p2)

    elif b == 'Axe':
        print ('You have struck the knight!  His health is now', knight - randint(2,6))
        print ('He has hit you as well!  Your health is now', player - randint(3,4))

    elif b == 'Mace':
        print ('You have struck the knight!  His health is now', knight - randint(0,7))
        print ('He has hit you as well!  Your health is now', player - randint(2,6))

    if player <= 0: 
        print ('You have died...')
    elif knight <= 0:
        print ('You have won!')

    break

print ('Good Game')
提问于
用户回答回答于

计算结果knight - randint(2,6)不会改变骑士的价值。

我想你可能会想要这样的东西:

damage = randint(2, 6)
knight = knight - damage
用户回答回答于

除非当玩家拿起剑,你不会在任何时候改变你的健康价值。在输出信息时,还没有设置变量。

扫码关注云+社区

领取腾讯云代金券