Danger Cat
Danger Cat

Reputation: 39

Exiting a function using only basic programming

I am working on my assignment, and I need to exit a function if the value returns True. It's a go fish game (you have all been a great help so far!) and I'm trying to figure out how to exit a function.

def TargetPlayer(player_number, pHands,sDeck):
"""User inputs which player they are choosing as the target player, returns
target players number"""
    gameoverhands = GameOverHands(pHands)
    if gameoverhands == True:
        **missing code here**
        return gameoverhands
    else:
        ShowMessage("TURN: Player " + str(player_number) + ", it's your turn. ")
        if player_number == 0:
            ask = raw_input("Who do you want to ask? (1-3) ")
            while not ask.isdigit() or ask not in "123":
            etc ....
        return other_values

I guess a thing to ask would be can you have different return statements that only return that value if the if statement is executed? gameoverhands is basically saying that you have no cards in your hand and the game is over, so i need to somehow jump directly to the final function in the game, whereas the else statement will (hopefully) execute the rest of the code repeatedly until gameover occurs. is this possible with very basic programming? any input would be fantastic

Upvotes: 0

Views: 96

Answers (2)

Harry Pehkonen
Harry Pehkonen

Reputation: 3038

It's good to have one single return statement in Python (as well as most other languages), but you can have other ones also. Mostly it's about making the code as readable as possible.

Here is a sample with one final return in the end:

def TargetPlayer(player_number, pHands,sDeck):
    """User inputs which player they are choosing as the target player, returns target players number"""
    result = GameOverHands(pHands)
    if gameoverhands == True:
        **missing code here**
        result = gameoverhands
    else:
        ShowMessage("TURN: Player " + str(player_number) + ", it's your turn. ")
        if player_number == 0:
            ask = raw_input("Who do you want to ask? (1-3) ")
            while not ask.isdigit() or ask not in "123":
            etc ....
            result = "Thisnthat"
    return result

This re-defines an "inner" function:

def outerfunc(cond):
    def inner1():
        print('inner1')
    def inner2():
        print('inner2')
    if cond:
        chosenfunc = inner1
    else:
        chosenfunc = inner2
    chosenfunc()

outerfunc(True)
outerfunc(False)

Upvotes: 1

sjakobi
sjakobi

Reputation: 3606

Why don't you just call the final function if gameoverhands == True (or rather if gameoverhands)?

def TargetPlayer(player_number, pHands,sDeck):
"""User inputs which player they are choosing as the target player, returns
target players number"""
    gameoverhands = GameOverHands(pHands)
    if gameoverhands == True:
        final_function()
    else:
        ShowMessage("TURN: Player " + str(player_number) + ", it's your turn. ")
        if player_number == 0:
            ask = raw_input("Who do you want to ask? (1-3) ")
            while not ask.isdigit() or ask not in "123":
            etc ....
        return other_values

In this case final_function should be something like this:

def final_function():
    print "Goodbye!"
    sys.exit()

Upvotes: 0

Related Questions