#π return
37 messages Β· Page 1 of 1 (latest)
@glass socket
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.
wdym
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")
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?
bool mean return True/False, then you shouldn't raise exception inside it
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
.
I don't think the semantics are good
if it just decide to stop game or not, I'd return nothing
I see return True mean nothing in this context if we raise StopGame
yes but if we dont I mean
then replace None to False
I see
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.