From 75e185e43789d08d1b7dbacaeaf3ab79e856bf41 Mon Sep 17 00:00:00 2001 From: caitsith2 Date: Thu, 18 Jun 2020 08:51:38 -0700 Subject: [PATCH] Allow specififying returned default values. Disallow ALL zero weights. --- Mystery.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Mystery.py b/Mystery.py index 173cf334..2c91d74f 100644 --- a/Mystery.py +++ b/Mystery.py @@ -223,15 +223,17 @@ def convert_to_on_off(value): return {True: "on", False: "off"}.get(value, value) -def get_choice(option, root) -> typing.Any: +def get_choice(option, root, value=None) -> typing.Any: if option not in root: - return None + return value if type(root[option]) is not dict: return interpret_on_off(root[option]) if not root[option]: - return None + return value + if any(list(map(int, root[option].values()))): return interpret_on_off( random.choices(list(root[option].keys()), weights=list(map(int, root[option].values())))[0]) + raise RuntimeError(f"All options specified in {option} are weighted as zero.") def handle_name(name: str):