I've created this code to show what books people haven't read at a Bookclub.
The code I have written is
Books = {}
Names = []
temp1 = set()
line = input('Book read: ')
while line:
Book, Name = line.split(':')
if Book not in Books:
# add it to our dictionary as a list with one element
Books[Book] = [Name]
else:
Books[Book].append(Name)
line = input('Book read: ')
if Name not in Names :
Names.append(Name)
Names = list(dict.fromkeys(Names))
for Name in Names:
temp1.add(Name)
BookSorted = dict(sorted(Books.items()))
for Book in BookSorted:
if BookSorted[Book] == Names :
print(Book + ": Everyone has read this!")
else:
temp2 = set(BookSorted[Book])
print(Book, ':', ', '.join(temp1 - temp2))
Which does work, but both the list of books and the names don't come out in alphabetical order. (I have removed my various attempts so you can read it easier).
When presented with the following test sample
Book read: Pride and Prejudice:Jenny
Book read: A Tale of Two Cities:Mark
Book read: Magician:Jenny
Book read: The Lord of the Rings:Pavel
Book read: Magician:Pavel
My code returns
Pride and Prejudice: Mark, Pavel
A Tale of Two Cities: Pavel, Jenny
Magician: Mark
The Lord of the Rings: Mark, Jenny
When it should return
A Tale of Two Cities: Jenny, Pavel
Magician: Mark
Pride and Prejudice: Mark, Pavel
The Lord of the Rings: Jenny, Mark
Any help would be much appreciated :)
Kindest Regards