From fde45a24174877c61d43d49969aeb51bf110d2c5 Mon Sep 17 00:00:00 2001 From: isti Date: Tue, 13 Jul 2021 23:38:40 +0200 Subject: [PATCH] Ask for confirmation before creating a new category --- linkulator.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/linkulator.py b/linkulator.py index 7a9ea66..92e9508 100755 --- a/linkulator.py +++ b/linkulator.py @@ -273,6 +273,14 @@ def is_valid_input(entry: str) -> bool: return False return True +def is_correct_category(entry: str) -> bool: + """Make sure the user purposefully created a new category and not by + accident (mistyped, tried to use category number instead of name)""" + if entry not in [ record["name"] for record in categories ]: + question = "Do you want to create a new category '{}'? Y/[N]".format(entry) + answer = input(question).lower().strip() + return answer != "" and answer[0] == "y" + return True def get_input(item: str) -> str: """Get user input with the specified prompt, validate and return it, or @@ -312,6 +320,8 @@ def post_link() -> int: try: url = get_input("URL") category = get_input("Category") + while not is_correct_category(category): + category = get_input("Category") title = get_input("Title") except ValueError: print("Post cancelled")