1

I'm just beginning to learn coding, so I'm just looking for hints at this point. The code is as followed:

import math
import random
import time
import sys
health = 100
ophealth = 80
player = False
opponent = False
# Took a code from sebastian ( def print_slow(str) part )
# Author: https://stackoverflow.com/users/1366738/sebastian
# Reference: https://stackoverflow.com/questions/4099422/printing-slowly-simulate-typing  
def print_slow(str):
    for letter in str:
        sys.stdout.write(letter)
        sys.stdout.flush()
        time.sleep(0.1)
def healthbar(health):
    return str(health)
def healthbar(ophealth):
    return str(ophealth)
def damaged(health):
    return health - 20
    health = health - 20
def damaged(ophealth):
    return ophealth - 20
    ophealth = ophealth - 20
gender = str(input('Hello! what gender are you? male or female?\n'))
name = str(input('Hm... whats your name?\n'))
age = str(input('and how old are you?\n'))
print_slow('alright! your registerd as ' + name + ", " + age + " years old, and " + gender + "!!\n")
ushieldhitcount = 0
mshieldhitcount = 0
def play():
    if ushieldhitcount >= 1 0:
        print('your shield broke! when your shield broke, tiny part of metal got in your eye and the monster killed you! YOU LOST')
        exit()
    if mshieldhitcount >= 10:
        print('the monsters shield broke and a piece got in the monsters eye, and you killed the monster while it was blinded!!! YOU WIN')
        exit()
    monster = random.choice(['att','def'])
    player = input('welcome! enter to continue\n')
    print_slow('You ran into a monster!\n')
    print('att to attack\n')
    print('def to defend\n')
    print('you have ' + str(healthbar(health)) + ' and the monster has ' + str(healthbar(ophealth)) + '\n') 
    player = input('run to attempt to run\n')
    if player == 'att' and monster == 'att':
        print_slow('the monster also chose to att! your swords clashed into each other!')
    if player == 'att' and monster == 'def':
        print_slow('you attacked! but they shielded! dealing 0 damage\n')
        print('you have ' + str(healthbar(health)) + ' and the monster has ' + str(health(ophealth))+'\n')
        mshieldhitcount = mshieldhitcount + 1
    if player == 'def' and monster == 'att':
        print_slow('you defended! and the monster attacked! dealing you 0 health')
        ushieldhitcount = ushieldhitcount + 1
    if player == 'def' and monster == 'def':
        print_slow('LMAO, you guys both defended dealing each other NOTHING')
        print('you have ' + str(healthbar(health)) + ' and the monster has ' + str(healthbar(ophealth)) + '\n') 
    
print(play())

On compile, it reports

UnboundLocalError: local variable 'ushieldhitcount' referenced before assignment.

I tried many things to fix this but I don't really know how. Can someone help me out? please do not comment the answer, and give a hint if you could. I want to get hints and do it on my own so I can improve. Thank you very much for reading all this!

MattDMo
  • 100,794
  • 21
  • 241
  • 231

2 Answers2

0

after line def play(): put

global ushieldhitcount

also

global mshieldhitcount

also remove one of the duplicated functions

and also another thing after return line, no commands executed

Erfan Paslar
  • 110
  • 1
  • 8
0

The problem here is that the variables ushieldhitcount and mshieldhitcount

are outside the function play()

to use a variable that is defined outside a function,we use the keyword global

try this:

def play():
global ushieldhitcount
global mshieldhitcount 
if ushieldhitcount >= 10:
    print('your shield broke! when your shield broke, tiny part of metal got in your eye and the monster killed you! YOU LOST')
    return
if mshieldhitcount >= 10:
    print('the monsters shield broke and a piece got in the monsters eye, and you killed the monster while it was blinded!!! YOU WIN')
    return

with the rest of your code...

if you have any more problems let me know!

Moaz_pr
  • 31
  • 7