擅长:python、mysql、java
<p>下面是一个清理过的代码版本,它可以很好地工作,而不会像您提到的那样出现循环:</p>
<pre><code>import random, sys
def enemyHP():
global enemyhealth, playerhealth, dmg, alcohol
if enemyhealth > 0 and playerhealth > 0:
if alcohol == 1:
miss = random.randint(0,2)
print("The alcohol makes you more furious...")
dmg = 0 if miss == 0 else dmg*2
enemyhealth -= dmg
if enemyhealth <= 0:
print("you won")
sys.exit()
else:
print("you did {} damage \nenemy health is: {}".format(dmg, enemyhealth))
# Your variables
enemyhealth = 1
playerhealth = 2
dmg = 1
alcohol = 1
# Call the function
enemyHP()
</code></pre>
<p><strong>样本输出:</strong></p>
<pre><code>The alcohol makes you more furious...
you did 0 damage
enemy health is: 1
</code></pre>
<p>这是基于上述变量赋值</p>