I'm currently attempting to write the second part of a quiz program, which uses a simple function load_quiz
to load a .json file containing the quiz data, and if a file is not found or is empty it will simply return nothing and refuse playing the quiz.
My code is as follows:
def load_quiz():
quiz = None
try:
with open('quiz.json', 'r') as f:
quiz = json.load(f)
except (OSError, ValueError):
print("Quiz file not found/empty! Please load an unemptied quiz .JSON in order to play.")
return None
finally:
if quiz and quiz[playable]:
play_quiz(f)
else:
print("Quiz file invalid! Please load a valid .JSON in order to play.")
return None
What I'm wondering is if this approach would even work. If the function returns None
within the except
block, will that still execute whatever is in the finally
block, or will the function simply stop there? If not, then how would I go around that?
Is this a viable option?