Problem questions that you use, such as Python functions, dictionaries, and repetitions.

Asked 2 years ago, Updated 2 years ago, 16 views

 Hong Gil-dong = list()
Hong Gil = list()
Hong Gil-geum = list()
book = {1:"Dore Miserables", 2:"Great Sotsby", 3:"Adult Prince", 4:"Child and Mountain", 5:"Insect Farm",
        6:"The Warring States Period", 7:"The Apology of Fury", 8:"Steel Book", 9:"The First Leaf", 10:"And Said Anything"}
borrower = {}

def borrow():
    cnt = 0
    book_list = []
    while cnt !=3:
        thisbook = input ("Code of book to borrow:")
        if thisbook =="q":
            break
        book_list.append(book[int(thisbook)])
        cnt = cnt + 1
    return book_list

while True:
    a = input ("Enter a member name").:")
    if a == 'q':
        break
    borrower[a] = borrow()

print ("Current Borrower List")
print(borrower)

With this code, the list of borrowers is

{'Hong Gil-dong':['Dore Miserables', 'Adult Prince'] 'Hong Gil':[추춘시대The Warring States Period
, "The Apology of Fury", "The Great Sotsby"]}

It is printed as shown in . By modifying this code, if the code of the same book is entered twice, I would like to print out 'It is already a borrowed book'. Therefore, I would like to receive the book code that I want to borrow through input and delete it from the book dictionary through del and then re-enter it. When you receive a key that is not in the book dictionary when you receive it again, you output 'This book is already borrowed'. So, the def part in the middle

def borrow():
    cnt = 0
    book_list = []
    while cnt !=3:
        thisbook = input ("Code of book to borrow:")
        delbook[thisbook] # The part I inserted------------
        if thisbook not in book:
            print("This is a borrowed book.") #-----------------
        if thisbook =="q":
            break
        book_list.append(book[int(thisbook)])
        cnt = cnt + 1
    return book_list

I got an error when I changed it like this. Actually, I have a feeling that it's a wrong code, but I don't know what to do. I'm looking for help.

python

2022-09-20 22:09

1 Answers

 Hong Gil-dong = list()
Hong Gil = list()
Hong Gil-geum = list()
book = {1:["Dore Miserables", 1], 2:["Great Sotsby", 1], 3:["Adult Prince", 1], 4:["Child and Mountain", 1], 5:[Insect Farm", 1],
        6:["The Spring and Autumn Warring States Period",1],7:[The Apology of Fury",1],8:["Steel North",1],9:["The First Leaf",1],10:["And Said Anything",1]}
borrower = {}

def borrow():
    cnt = 0
    book_list = []
    while cnt !=3:
        thisbook = input ("Code of book to borrow:")
        if thisbook =="q":
            break
        else:
            if book[int(thisbook)][1] != 0:
                book_list.append(book[int(thisbook)][0])
                book[int(thisbook)][1] -= 1
                cnt = cnt + 1
            else:
                print ("Please re-enter the book because it is not available")
    return book_list
#Hereinafter omitted

I think it would be nice to do it this way


2022-09-20 22:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.