Mordalvil
Mordalvil

Reputation: 43

Dividing by two outputs 0 instead of the desired result

In my RPG fight code, if you use the check function, it should half your armour, but it instead reduces it to 0.

I have tried replicating the issue in another python file, but it worked fine and I cannot tell why this is occurring.

I have put a comment in the code to show where the problem occurs. (above 'elif choiceAttack == 3:') The problem is with the armour variable.

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.")

I expect your armour to be halved when you press 3, but it reduces it to 0. I have probably done something dumb, which is why it won't work, but please point it out to me anyway!

Thank you in advance for your help!

Upvotes: 0

Views: 47

Answers (1)

LcdDrm
LcdDrm

Reputation: 1009

Programmatically it's not wrong. If you press 3:

  1. Your armor gets halfed. Starting at 10, it's now 5
  2. The enemy attacks. Your current armor of 5 is reduced by the enemies attack rating, which is 5.

In total this results in your armor being reduced to 0.

Upvotes: 1

Related Questions