0

This is the code.

#When I try to make the variable ‘enemy’ save globally, it says ‘Variable referenced before assignment.’, or something along those lines. How do I fix this?

#If you are going to suggest to make it all one function, I tried and it didn't work out. I am doing this so I can keep repeating the turn function until the player enters a vaild command.

#Player and enemy stat blocks. Order of stat blocks is: Health, Weapon, Weapon damage, Armor, and Armor type.

PC = [50, 'shortsword', 20, 'chain mail', 10]
goblin = [30, 'dagger', 15, 'leather', 5]
# Function for a single turn.
def turn(enemy):
    turn1 = input('Your turn. What would you like to do? ').lower()
    if turn1 != 'attack':
        print('Invalid command.')
        turn(enemy)
    elif turn == 'attack':
        global enemy
        enemy[0] = enemy[0] - PC[2]
    
# Function for the entirety of combat.  
def combat(enemy):
    while enemy[0] > 0:
        turn1 = input('Your turn. What would you like to do?')
        turn(enemy) 
    
    
combat(goblin)
Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
  • Does this answer your question? [UnboundLocalError: local variable 'text' referenced before assignment](https://stackoverflow.com/questions/62668454/unboundlocalerror-local-variable-text-referenced-before-assignment) – ChrisGPT was on strike Jan 11 '21 at 03:12
  • Does this answer your question? [Python function parameter as a global variable](https://stackoverflow.com/questions/9242812/python-function-parameter-as-a-global-variable) – Brian McCutchon Jan 11 '21 at 04:18
  • Actually, are you just trying to modify the passed-in `enemy` list? If so, just delete `global enemy`. You don't need `global` for that. – Brian McCutchon Jan 11 '21 at 04:24

0 Answers0