首页 > 解决方案 > Python,代码重复自身直到某个事件

问题描述

我做了一个小游戏,你可以杀死兽人。您选择攻击或治疗等。我是 python 新手。我似乎无法让代码循环,所以它一直持续到兽人或你死去。任何解决方案/提示?

from time import sleep
from random import randint
#here
game = True
health = 100
feeling_brave = True
playerturn = True
attackdmg = randint(2,4)
maxhealth = health
heal = randint(3,5)
playerab = 0
ahealth = randint(10,70)
amaxhealth = ahealth
attackadmg = randint(1,3)
orcturn = False


sssprint('Kill the orc! He has', (ahealth), ('health. '))
sleep(1)

while game == True:

    if playerturn == True:
        playerab = input('Attack or Heal? ')

        if playerab == ('Attack'):
            sleep(1)
            print('You decide to attack the Orc.')
            sleep(1)
            ahealth = ahealth - attackdmg
            print('You hit the orc for', (attackdmg))
            sleep(1)
            print('The Orc has', (ahealth), ('health remaining.'))
            orcturn = True
            playerturn = False

    if playerab == ('Heal') and health != maxhealth and playerturn == True:
        print('You cast a healing spell on yourself')



    if orcturn == True:
         sleep(1)
         print('Its the orcs turn to strike!')
         if ahealth > 9:
             sleep(1)
             health =  health - attackadmg
             print('The orc strikes you for', (attackadmg))
             print('You have', (health), 'health left')
             orcturn = False
             splayerturn = True

任何帮助都会很好:D

标签: python

解决方案


修复了和之前的 playerturn 变量一样的问题!我是个白痴哈哈


推荐阅读