From e6d2521da126c6a3f96aa3898a3bb6893280cd1e Mon Sep 17 00:00:00 2001 From: Egor Blagov Date: Sun, 18 Dec 2022 19:35:43 +0400 Subject: [PATCH] Add choose at the end - now we can repeat or exit --- main.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index d7c7579..ccf3dd2 100644 --- a/main.py +++ b/main.py @@ -17,7 +17,10 @@ def input(self, prompt: str | None = None) -> str: @abc.abstractmethod def print(self, message: str) -> None: pass - + + @abc.abstractmethod + def choose(self, *options: str) -> str: + pass class CliApi(IoApi): def input(self, prompt: str | None = None) -> str: @@ -26,6 +29,17 @@ def input(self, prompt: str | None = None) -> str: def print(self, message: str) -> None: print(message) + def choose(self, *options: str) -> str: + while True: + try: + option_lines = [f"{i}) {opt}" for i, opt in enumerate(options)] + self.print("\n".join(option_lines)) + user_input = self.input("Enter index: ") + return options[int(user_input)] + except KeyboardInterrupt: + raise + except: + self.print("Try again") def weather_query(io: IoApi): while True: @@ -57,5 +71,13 @@ def weather_query(io: IoApi): f"Current temperature is {response['current_weather']['temperature']} °C" ) + CONTINUE, QUIT = "continue", "quit" + + choice = io.choose(CONTINUE, QUIT) + if choice == QUIT: + io.print("Terminating") + break + + io = CliApi() weather_query(io)