different errors, return outside function and others

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Feb 23 09:23:25 CET 2009


En Mon, 23 Feb 2009 04:51:39 -0200, Tony <sternbrightblade at gmail.com>  
escribió:

> Hi,
> I am trying to write a small program for my final for school, everytime i
> run my program it gives me a return outside function error (after i fixed
> all the indentation errors it throws at me), i am not very good at
> programing with python atm, so any help would be appreiciated. and if im
> missing somethign please feel free to throw
> any suggestions out there.

Is this your *actual* code? It does not even compile...
Things like 'def foo(...):' are *functions*, not *modules*.

Variables inside functions (including its arguments) are locals, private  
to the function. Even if you modify them, the "outside world" doesn't  
notice. So a function like this:

> def fight(HP, ZHP, weapon, boss=False):
>     if weapon == 'fist':
>         while HP > 0 and ZHP > 0:
>             damage = 2
>             ZHP = ZHP - damage
>             if boss:
>                 HP = HP - dice_roll2()
>             else:
>                 HP = HP - 2
>     elif weapon == 'baseball bat':
           [... more code ...]

won't alter the "global" values HP, ZHP; in fact, it won't do nothing.

Either return the new values:
    return HP, ZHP
and also adjust everywhere you call:
    HP, ZHP = fight(HP, ZHP, ...)

Or mark HP, ZHP as globals (using the global statement).

-- 
Gabriel Genellina




More information about the Python-list mailing list