0

Write a sequence of statements that prints the title of each book in BSI, one per line, in alphabetical order; do this without changing the original order of BSI. My code:

 from collections import namedtuple
 Book = namedtuple('Book', 'author title genre year price instock')

baby = Book('Tyler Hawkin','ABC','education',2009, 20.00, 75) 
child = Book('Miss Hanigan','Jannie','adventure',1900, 26.00, 51)
preteen = Book('Leila Star','My first crush','comedy',2013, 8.89, 11) 
teen = Book('John Green', 'Fault in our Stars', 'romance',2006, 17.00,0)
adult = Book('Shakespeare', 'Romeo', 'Drama', 1610, 5.00, 99)
elderly = Book('Janett Smith','how to be young again','life',1995, 13.00, 3) 
BSI = [baby,child,preteen,teen,adult,elderly]

def book_title(book:Book)->str:
    return book.title

alpha_BSI = sorted(BSI, key=book_title)
    for i in range(len(BSI)):
        print(alpha_BSI[i].title)

My code outputs the book titles but not alphabetically.

anonymous fox
  • 39
  • 1
  • 5

1 Answers1

0

I figured it out, my code was correct, I just thought it wasn't because one of my strings began not capitalized, and when using sort() or sorted(), it also sorts by capitalization.

anonymous fox
  • 39
  • 1
  • 5