this is my first time trying to write a python code that is actually usable, and although it might be too simple or maybe too messy (for a python code standards) to share, yet there nothing wrong with trying to learn your flaws at an early stage right ?😄. so I wish you guys can give some feedback on it, like how can I make it more pythonic or more professional looking ?
it's a simple program of finding a book in a specific library and knowing its genre
comedy = ["idiots","Boys", "Running"]
romance = ["lovers", "long story", "rings"]
horror = ["bells", "house", "faceless"]
Library = {"comedy": comedy, "Romance": romance, "Horror": horror}
user_input = input("please enter the books you want: ")
found = False
for genre in Library:
for book in Library[genre]:
if user_input == book:
found = True
print ("Book:", book)
print ("Genre:", genre)
if not found:
print ("the book was not found")
Mark
Looks good!
Only
Libraryis usually justlibrarysince it's not a class.You could also use
into replace one of the loops, which is not to say it's better, but easier (didn't try, but something like this):for genre, books in library.items(): if book in books: found = True print ("Book:", book) print ("Genre:", genre)That will just check if the book is in the list.
(If you have a lot of books, this would be faster if you used sets instead of lists.)