Home > Software engineering >  How to skip a function call from another function if a condition is met
How to skip a function call from another function if a condition is met

Time:07-09

I'm writing a game and I need to skip the MOBTURN() if and only if the player succeeds at the flee roll.

My code pertaining:

   def PLAYERFLEE():
        fleechance=random.randint(1,10)
        global mhp
        global mgold
        global php
        if fleechance>=escapechance:
            mhp=int(0)
            mgold=int(0)
            print('You fled')
            
        else:
            fleedmg=(random.randint(1,10) mrdmg)
            php=php-fleedmg
            print(f'You took {fleedmg} damage!')


    def MOBTURN():
        mobatk=random.randint(1,10) mdmg-parmor
        global php
        php=php-mobatk
        print(f'Mob HP:{mhp} Mob Damage:{mdmg} Mob Range Damage:{mrdmg} Mob Armor:{marmor} Mob Stun Status:{mstun}')
        print(f'You took {mobatk} damage!')


    def PLAYERTURN():
        action=input(f'Your stats are HP: {php}, DMG: {pdmg}, Range DMG: {prdmg}, Stun Status: {pstun}. What will you do? (atk/ratk/flee/stun)')
        if action==str('atk'):
            PLAYERATTACK()
        elif action==str('ratk'):
            PLAYERRANGEATTACK()
        elif action==str('flee'):
            PLAYERFLEE()
        elif action==str('stun'):
            PLAYERSTUN()
        else:
            print('Please choose an option from the parentheses!')
            PLAYERTURN()
        

    def FIGHT():
        mobalive=True
        while mobalive==True:
            PLAYERTURN()
            if mhp<=0:
                mobalive=False
            MOBTURN()

CodePudding user response:

Your code is quite unclear since it is missing a lot, however, I believe what you need to do is, wherever you check if the player succeeds at flee roll, return True. Only execute MOBTURN() if flee roll doesn't return True.

CodePudding user response:

... if and only if the player succeeds at the flee roll.

Return a value from the function then use a conditional statement to call MOBTURN

....

    def PLAYERFLEE():
        ....
        if fleechance>=escapechance:
            mhp=int(0)
            mgold=int(0)
            print('You fled')
            return True
            
        else:
            fleedmg=(random.randint(1,10) mrdmg)
            php=php-fleedmg
            print(f'You took {fleedmg} damage!')
            return False
    ....

    def PLAYERTURN():
        ...
        elif action==str('flee'):
            return PLAYERFLEE()
        ...

    def FIGHT():
        ...
        while mobalive==True:
            success = PLAYERTURN()
            ...
            if success: MOBTURN()
            ...

In my example of PLAYERTURN the other cases will return None unless you specify something.

  • Related