From d985f5f282c0330aafab9d42ffd9f1f0f3a859f0 Mon Sep 17 00:00:00 2001 From: DietermiGamzD125 Date: Tue, 8 Oct 2024 21:23:14 +0100 Subject: [PATCH] Handle ValueError and await user input Made it so the user has to press a key before it continues, making it more readable. Also the code will keep running even if the user inputs something that's not an integer. --- Main.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Main.py b/Main.py index dedc4a3..4e797da 100644 --- a/Main.py +++ b/Main.py @@ -43,17 +43,29 @@ def returnBook(self): 4. Exit the Library ''' print(welcomeMsg) - a = int(input("Enter a choice: ")) + choice = 0 + while choice == 0: + try: + a = int(input("Enter a choice: ")) + except ValueError: + print("Invalid input!") + input() + else: + choice = 1 if a == 1: centraLibrary.displayAvailableBooks() + input() elif a == 2: centraLibrary.borrowBook(student.requestBook()) + input() elif a == 3: centraLibrary.returnBook(student.returnBook()) + input() elif a == 4: print("Thanks for choosing Central Library. Have a great day ahead!") exit() else: print("Invalid Choice!") + input()