#πŸ”’ return

37 messages Β· Page 1 of 1 (latest)

glass socket
#

hello I am trying to make a stop command in my loop and it just doesnt break the loop...
I know that I could type return manually all the time but I want to know if its possible to do it with another func

code: https://paste.pythondiscord.com/C2MQ

cunning wolfBOT
#

@glass socket

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

glass socket
#

wdym

reef light
#

well if you wanna use custom exception

class StopGame(Exception):
    pass


def stop_game():
    ...
    raise StopGame()


def play_game():
    try:
        while True:
            ...
            while True:
                ...
                while True:
                    ...
                    stop_game()
    except StopGame:
        pass

    print("Game over")
glass socket
#

omg

#

I think I found an easier way xD

#
def stop_game() -> bool:

    """Stop the game.
    
    The function will ask the user if they want to leave,
    if they do : it will return True and stop the game.

    :return: True or None
    """

    confirm_exit = input("Are you you want to exit the game? \nType 'yes' to confirm: ")

    if (confirm_exit.lower() == "yes"): #confirm the user's choice.
        return True
#
if stop_game():
                        playing_game = False #breaks the loop and exits the game.
                        return
#

btw

#

do u know if I typed the stop_game func correctly?

#

like annotations and stuff

#

is return True or None correct?

reef light
#

bool mean return True/False, then you shouldn't raise exception inside it

glass socket
#

uhm

#

what should I write?

reef light
#

like this

def stop_game() -> bool: ...


def play_game():
    try:
        while True:
            ...
            while True:
                ...
                if stop_game():
                    raise StopGame()
    except StopGame:
        pass
#

I'm not sure

glass socket
#

yes but I was talking about the stop_game func

#

Is it correctly written?

glass socket
reef light
#

I don't think the semantics are good

#

if it just decide to stop game or not, I'd return nothing

glass socket
#

hmmm

#

but it returns True

#

tho if I say no

#

it doesnt return anything

reef light
#

I see return True mean nothing in this context if we raise StopGame

glass socket
#

yes but if we dont I mean

reef light
#

then replace None to False

glass socket
#

I see

reef light
#

just say return bool

#

who care it is None or False, both are Falsy value πŸ˜„

glass socket
#

xDDD

#

tyyy

#

!close

cunning wolfBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.