Hello everyone, I'm back again. I've got a very minor problem! Make it rhyme.
# Now dictionaries are involved, everything becomes way easier.
def return_book():
while True:
# Turns out books dictionary into a list, called genres
genres = list(books)
# prints out the selection menu
print("Please enter a number to select your genre. To exit, enter 0: ")
# uses enumeration, sets start to 1 so that it starts at 1 and not 0. We will need to subtract 1 from their answer for genre.
for index, genre in enumerate(genres, 1):
# F string prints our index and genre
print(f'{index}. {genre}')
selection = int(input('> '))
if selection == 0:
print("Exit!")
break
# Len is the count of the objects, so if we put a number higher than the count...it prints an error.
elif selection > len(genres):
print("Invalid!")
else:
# selected_genre becomes a key with which we can access our dictionary
print("This was a valid selection.")
# subtracts 1 from the genres so that user input lines up with dictionary.
selected_genre = genres[selection - 1]
if books[selected_genre] < MAXIMUM_STOCK[selected_genre]:
books[selected_genre] += 1
# Our f-string lets us print the genre and how many books are in it.
print(f"You have successfully returned a {selected_genre} book.")
print(f"You now have {books[selected_genre]} {selected_genre} book(s)")
break
else:
print(f"Error 5 ({selection}): Returning exceeds inventory maximum.")```
I need to fit an `except ValueError` in here somewhere, I think, so that if someone prints some garbage text, it doesn't break and go to `main_menu` but instead restarts the loop. Don't know how to fit it here though, cheers!