python modifying a variable with a function
<p>I am trying to write a function for a basic python course that I am taking. We are at the point where we join as a group and make one program as a team. I have assigned each member to write their portion as a function in the hopes that I can just call each function to perform the overall program. It has been a while since I played in object programing and this is above the class requirements but I want to try and make this work.</p>

<p>I am having difficulty passing a variable into a function then retrieving the changed variable from the function. </p>

<p>I have tried reading on multiple sites and deep search on here but I am missing something and any assistance would be greatly appreciated.</p>

<p>Here is the first sample of code I tried</p>

<pre><code>import random
MOBhp=100

def ATTACK(TYPEatck,MOBhp):
#    global MOBhp

    print ('the type attack chosen was ',TYPEatck,'the MOB has ',MOBhp)
    if TYPEatck =='M'or TYPEatck =='m':
        print ('the ',PLAYER,' used melee to attack',MOB)
        MOBhp=MOBhp-10
    elif TYPEatck =='R'or TYPEatck =='r':
        print ('the ',PLAYER,' used range to attack',MOB)
        MOBhp=MOBhp-5
    else:
        print ('please choose a valid attack')
    print ('the MOB hitpoints are ',MOBhp)
    return MOBhp;


PLAYER='HERO'
MOB='Dragon'
AC=12
while MOBhp &gt; 0:
   TYPEatck=random.choice('RM')
   ATTACK(TYPEatck,MOBhp)
print('really the MOB hitpoints are ', MOBhp)        
print(MOB,'was slain by ',PLAYER)
</code></pre>

<p>This gives a repeating result that I have to break with cntrl+c</p>

<pre><code>the type attack chosen was  R the MOB has  100
the  HERO  used range to attack Dragon
the MOB hitpoints are  95
the type attack chosen was  M the MOB has  100
the  HERO  used melee to attack Dragon
the MOB hitpoints are  90
the type attack chosen was  R the MOB has  100
the  HERO  used range to attack Dragon
the MOB hitpoints are  95
</code></pre>

<p>Where as if I do the following</p>

<pre><code>enter code here
#while MOBhp &gt; 0:
TYPEatck=random.choice('RM')
ATTACK(TYPEatck,MOBhp)
print('really the MOB hitpoints are ', MOBhp)        
print(MOB,'was slain by ',PLAYER)
</code></pre>

<p>I get the following results</p>

<pre><code>the type attack chosen was  R the MOB has  100
the  HERO  used range to attack Dragon
the MOB hitpoints are  95
really the MOB hitpoints are  100
Dragon was slain by  HERO
</code></pre>

<p>I have tried to play with global variables as well and can't seem to get that to work either.</p>

<p>Thank you all for you assistance! I figured it out and was able to write my program to completion. </p>

<p><a href="https://www.reddit.com/r/Python/comments/578b6e/0300/" rel="nofollow">Here is the code formatted and shared on Reddit</a></p>

python function variables