From c095d1d330c8ad4dacf6dbc77e184d77fa14b8a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abdulkadir=20Furkan=20=C5=9Eanl=C4=B1?= Date: Thu, 17 Jan 2019 17:19:22 +0100 Subject: [PATCH] Functionalize exit sequence --- ib-clearance.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/ib-clearance.py b/ib-clearance.py index ba18f3e..4ceef8f 100755 --- a/ib-clearance.py +++ b/ib-clearance.py @@ -147,6 +147,20 @@ def parse_time_string(timestring): return structtime +def exit_sequence(): + """Initiate exit sequence. + + Asks user to enter character, and exits program if X is pressed. + """ + print("\nPress X to exit.") + key = getch() + if isinstance(key, bytes): + # msvcrt.getch returns type bytes, decode into str. + key = key.decode() + if key.upper() == "X": + exit() + + def main(): print("ib-clearance") print("============") @@ -166,13 +180,7 @@ def main(): else: print("\nStudent still has lessons, clearance not granted.") - print("\nPress X key to exit, other key to run again.") - key = getch() - if isinstance(key, bytes): - # msvcrt.getch returns type bytes, decode into str. - key = key.decode() - if key.upper() == "X": - break + exit_sequence() if __name__ == "__main__":