From 8791bfb60dd7898a118d72ae19555ea51910ba5b Mon Sep 17 00:00:00 2001 From: Dave Molk Date: Sat, 22 Oct 2022 13:39:29 -0600 Subject: [PATCH] formatting d.py --- python/dj/d.py | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/python/dj/d.py b/python/dj/d.py index 1a89608..0ec937e 100644 --- a/python/dj/d.py +++ b/python/dj/d.py @@ -37,25 +37,26 @@ def get_user_input(): return args.joke, args.term def prep_format(joke: str): - if "?" in joke: - format(joke, "?") + joke = joke.replace('"', "") + if "?" in joke[:-1]: + setup_punchline(joke, "?") elif "." in joke[:-1]: - format(joke, ".") + setup_punchline(joke, ".") + else: + print(joke) -def format(text, splitter): +def setup_punchline(text, splitter): split_text = text.split(splitter) - print(split_text[0] + splitter + "\n") - time.sleep(2) - print(split_text[1].strip()) - -class BaseException(Exception): - pass - -class JSONException(BaseException): - pass + if len(split_text) == 1: + print(text) + else: + print(split_text[0] + splitter + "\n") + time.sleep(2) + if splitter == ".": + print(split_text[1].strip() + ".") + else: + print(split_text[1].strip()) -class NoJokesFoundException(BaseException): - pass if __name__ == "__main__": random_url = "https://icanhazdadjoke.com/" @@ -70,14 +71,9 @@ class NoJokesFoundException(BaseException): if results == "": print("no jokes for that term") else: - try: - rando = random.randint(0, len(results["results"]) - 1) - joke = results["results"][rando]["joke"] - except JSONException: - print("") - else: - prep_format(joke) - + rando = random.randint(0, len(results["results"]) - 1) + joke = results["results"][rando]["joke"] + prep_format(joke) else: headers = { "Accept": "text/plain",