除以两个输出0而不是期望的结果

时间:2019-04-04 11:14:51

标签: python python-3.x

在我的RPG战斗代码中,如果使用检查功能,则装甲量应该会减少一半,但会降低至0。

我尝试将问题复制到另一个python文件中,但是工作正常,我无法知道为什么会发生这种情况。

我在代码中添加了注释,以显示问题出在哪里。 (在“ elif choiceAttack == 3:上方”)问题出在装甲变量上。

import random

hp = 20
armour = 10
attack = 5
enemyHP = 20
enemyattack = 5
enemyArmour = 10
fullArmour = 10

def fight(): 
  global hp, armour, attack, enemyHP, enemyattack, enemyArmour, fullArmour 

  print("HP =",hp      )
  print("Armour = ", armour    )
  print("Attack =", attack)
  print("---------------")
  print("1 - Slash, 2 - Stab, 3 - Check enemy stats, 4 - Heal")
  choiceAttack = int(input())

  #Slash
  if choiceAttack == 1:
    #unsuccesful
    if random.randint(0,100) <= 75:
      print("The enemy blocked your attack. 1 - Stop the enemy from dealing a counter-blow\n2 - Attempt to break the block and hit the enemy - the enemy could hit you")
      choiceSlash = int(input())
      if choiceSlash == 1:
        print("You stopped the enemy from hitting you, but you didn't deal any damage.")

      elif choiceSlash == 2:
        if random.randint(0,100) <= 50:
          print("You failed to hit the enemy and they hit you instead.")
        else:
          print("You were succesful and dealt your attack power to the enemy.")
          enemyHP = enemyHP - attack  

    #succesful
    else:
      print("You hit them, dealing double your attack power!")
      enemyHP = enemyHP - attack*2

  elif choiceAttack == 2:
    if enemyArmour > 0:
      enemyArmour = enemyArmour - attack
    else:
      enemyHP = enemyHP - attack

  #The issue is here with the 'armour = int(armour/2)' line... 
  elif choiceAttack == 3:
    print("You check the enemy's stats, but they are unclear to you. You increase your attack to prepare for it, but checking the enemy lowered your armour")
    armour = int(armour/2)

    attack = attack + 2  

  elif choiceAttack == 4:
    if armour > 0:
      print("You heal 5HP and restore your armour to its full potential")
      hp = hp + 5
      armour = fullArmour
    else:
      print("You heal 5HP, but you cannot restore broken armour")
      hp = hp+5

  if enemyArmour < 0:
    enemyArmour = 0
  print("EnemyArmour =",enemyArmour)
  print("EnemyHP =",enemyHP)
  print("\n\n\n")

def enemyfight():
  global hp, armour, attack, enemyHP, enemyattack, enemyArmour 
  if armour > 0:
      armour = armour - enemyattack
  else:
      hp = hp - enemyattack

  if armour < 0:
    armour = 0

while enemyHP > 0 and hp > 0:
  fight()
  enemyfight()

if enemyHP <= 0:
  print("You win!")
elif hp <= 0:
  print("Game over.")

我希望按3时您的护甲减半,但它将减至0。 我可能做过一些愚蠢的事情,这就是为什么它不起作用的原因,但是无论如何请向我指出!!

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

以编程方式没有错。如果按3:

  1. 您的装甲减半。从10开始,现在是5
  2. 敌人进攻。您当前的5点护甲会因敌人的攻击等级5而降低。

总的来说,这会使您的护甲降低到0。