Python Forum
Returned value not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Returned value not defined
#1
I tried to create a random leveling system for mobs in my game, however, when I tried to check the output of a returned value It shows un in the console as "Undefined". Does anyone know why and how to solve it? I am sorry if the function seems complicated.

def monsterpower():
    import random
    weakmonsterlevel = random.randrange(1, 10, 1)
    normalmonsterlevel = random.randrange(5, 20 ,1)
    strongmonsterlevel = random.randrange(10, 40, 1)
    bosslevel = random.randrange(20, 80,1)
    weakmonsterhealth = weakmonsterlevel*1 +10
    normalmonsterhealth = normalmonsterlevel*1 +10
    strongmonsterhealth = strongmonsterlevel*1 +10
    bosshealth = bosslevel*2 + 20
    weakmonsterdamage = 2
    normalmonsterdamage = 4
    strongmonsterdamage = 6
    bossdamage = 10
    return weakmonsterhealth, normalmonsterhealth, strongmonsterhealth, bosshealth, weakmonsterdamage, normalmonsterdamage, strongmonsterdamage, bossdamage

print(weakmonsterhealth)
This is the error I got when I tried to print a returned value
NameError: name 'weakmonsterhealth' is not defined

oh I forgot to put this line in the paragraph, there is suppose to be
monsterpower()
before [python]print(weakmonsterhealth)[/p
Reply
#2
First of all, you have to call the function before it will do anything. Second of all, variables defined in functions are not available outside the functions. Returning them is how you get them out, but you have to assign the function call. If you want a variable named 'weakmosterhealth' to exist outside the function, you need to do something like:

weakmonsterhealth, normalmonsterhealth, strongmonsterhealth, bosshealth, weakmonsterdamage, normalmonsterdamage, strongmonsterdamage, bossdamage = mosterpower()
print(weakmonsterhealth)
That's a mess, really. You have too many variables. You could just take the return value as a tuple:

mosterstats = monsterhealth()
print(monsterstats[0])
But really, you should look at putting all of that in a class, or at least a dictionary.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
The function returns something like that (17, 15, 25, 154, 2, 4, 6, 10). I don't think that this is something which can be easily interpreted say in two weeks. What order, what values etc.

To make things more readable you could return dictionary (as suggested by ichabod801) or namedtuple. Then you have labels associated to your values to comprehend their meaning (FYI: namedtuples from collections built-in module are lightweight, immutable and have nice clean syntax to access values, something like: monsterpower.bosslevel

I observed that you imported module inside the function. Is there particular reason to do so? If there is no such reason I suggest to stick to PEP8 - Imports:

Quote:Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020