Skip to content

Commit

Permalink
Add choose at the end
Browse files Browse the repository at this point in the history
- now we can repeat or exit
  • Loading branch information
EgorBlagov committed Dec 18, 2022
1 parent 6fe60a6 commit e6d2521
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion main.py
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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)

0 comments on commit e6d2521

Please sign in to comment.