Extract language-independent 'Simulator' class from woods example

This commit is contained in:
Joeri Exelmans 2024-10-29 11:20:32 +01:00
parent 590ce0b0b9
commit c738e8bcd1
3 changed files with 177 additions and 113 deletions

View file

@ -16,24 +16,3 @@ def pause():
print("press any key...")
input()
def choose(msg:str, options):
arr = []
for i, (key, result) in enumerate(options):
print(f" {i}. {key}")
arr.append(result)
if len(arr) == 0:
return
return __choose(msg, arr)
def __choose(msg: str, arr):
sys.stdout.write(f"{msg} ")
try:
raw = input()
choice = int(raw) # may raise ValueError
if choice >= 0 and choice < len(arr):
return arr[choice]
except ValueError:
pass
print("Invalid option")
return __choose(msg, arr)