0

First post so please be gentle...

I'm putting a counter to count the rounds for each of the battles below but even though I give counter a value of 1 as the code starts, but when I look to add to this later in my loop, an error message suggests I'm referencing it before a value has been assigned?

I've played around with where I place the counter but I don't want to put it inside a loop as it'll just reset to '1' everytime...

import random
import time
import os

def character():
  name = input("Name your Legend: ")
  type = input("Character Type (Human, Elf, Wizard, Orc): ")
  return name
  return type

def rollDice(sides):
  number = random.randint(1,sides)
  return number

def health():
  diceOne = rollDice(6)
  diceTwo = rollDice(12)
  healthStat = ((diceOne * diceTwo / 2) + 10)
  return healthStat

def strength():
  diceOne = rollDice(6)
  diceTwo = rollDice(12)
  strengthStat = ((diceOne * diceTwo / 2) + 12)
  return strengthStat

print("## WELCOME TO THE ARENA ##")
print()
time.sleep(1) 
counter = 1

while True:
  charOne = character()
  charOneHealth = health()
  charOneStrength = strength()
  print()
  time.sleep(1)
  print("All hail" , charOne)
  print("HEALTH: " , charOneHealth)
  print("STRENGTH: " , charOneStrength)
  print()
  print("And who are they battling?")
  print()
  charTwo = character()
  charTwoHealth = health()
  charTwoStrength = strength()
  print()
  print("Please welcome" , charTwo)
  print("HEALTH: " , charTwoHealth)
  print("STRENGTH: ", charTwoStrength)
  time.sleep(2)
  os.system("clear")
  break
  
def battle(charOneStrength, charTwoStrength, charOneHealth, charTwoHealth):
  counter += 1
  charOneDice = rollDice(6)
  charTwoDice = rollDice(6)
  damage = (charOneStrength - charTwoStrength) + 1
  if charOneDice > charTwoDice:
    print(charOne , "strikes!")
    print(charTwo , "takes a hit, with " , damage , "damage")
    charTwoDamage = charTwoHealth - damage
    return charTwoDamage
  elif charTwoDice > charOneDice:
    print(charTwo , "strikes!")
    print(charOne , "takes a hit, with " , damage , "damage")
    charOneDamage = charOneHealth - damage
    return charOneDamage
  elif charOneDice == charTwoDice:
    print("It's a draw!")
    draw = True
    return draw
  
while True:
  print("## TIME TO FIGHT! ##")
  print()
  print("Round" , counter)
  print()
  battle(charOneStrength, charTwoStrength, charOneHealth, charTwoHealth)
  print(charOne)
  print("HEALTH: " , charOneHealth)
  print()
  print(charTwo)
  print("HEALTH: " , charTwoHealth)
  print()
  if charOneHealth < 0:
    print("Oh no!" , charOne , "has died!")
    print(charTwo , "killed" , charOne , "in" , counter , "rounds")
    exit
  elif charTwoHealth< 0:
    print("Yikes!" , charTwo , "has died!")
    print(charOne , "killed" , charTwo , "in" , counter , "rounds")
    exit
  else:
    print("They go again...")
    continue

I've had a search on here but can't seem to find an exact answer that solves the above.

Qoelet
  • 9
  • 2
  • 1
    `counter += 1` inside `battle` will try to create a new (local) variable with the same name as the global one. Put `global counter` before that line – UnholySheep Oct 12 '22 at 12:11
  • 1
    Does this answer your question? [changing global variables within a function in python](https://stackoverflow.com/questions/51564669/changing-global-variables-within-a-function-in-python) – UnholySheep Oct 12 '22 at 12:12

0 Answers0