0

所以我被困在我项目的一部分上。在这个项目中,我们必须将库文件读入字典来创建类似的内容:

inventory = {author: "book title, quantity, price"}

我已经做到了。然后我们必须为

  1. 显示库,已排序。
  2. 为作者添加一本书。
  3. 改变一本书的数量。
  4. 计算图书馆的总金额。
  5. 退出。

我被困在第 3 部分,这是我到目前为止所拥有的,但我不知道如何检查书名是否在库存中,因为标题中的每个单词都是大写的。

    def changeQty(inventory):
        author = inventory.keys()
        book = inventory.values()
        lastName = raw_input("Enter the author's last name: ").capitalize()
        firstName = raw_input("Enter the author's first name: ").capitalize()
        check = lastName + ", " + firstName
        while check not in author:
            print "There is no author by that name in the library."    
            lastName = raw_input("Enter the author's last name: ").capitalize()
            firstName = raw_input("Enter the author's first name: ").capitalize()
            check = lastName + ", " + firstName

        #after here i am stuck trying to make the title of 1 OR MORE words all                   
        #capitalized, to check to see if the title is in reference.getTitle()
        #example: title of dickens, charles would be Hart Times
        title = raw_input("Enter the title: ")
        for info in book:
            for reference in info:
                while title not in reference.getTitle():
                    print "This book does not exist in the library."
                    title = raw_input("Enter the title: ")
    def main():
        inventory = {}
        test = readDatabase(inventory)
        done = False
        while not done:
            menuOpt = raw_input("Please choose a menu option: ")
            if menuOpt == '3':
                newQty = changeQty(inventory)
            elif menuOpt == '5':
                print "Thank you for choosing this program to view your inventory"
                print "Now exiting"
                done = True
            else:
                print str(menuOpt) + " is outside this program's options."
                print "Please choose again."
    main()

    class Book:
        #constructor
        def __init__(self, title, qty, price):
            self.title = str(title)
            self.qty = int(qty)
            self.price = float(price)

        #Accessors:
        def getTitle(self):
            return self.title

        def getQte(self):
            return self.qty

        def getPrice(self):
            return self.price

        def getTotal(self):
            return self.price * self.qty

        #Mutators:
        def setQty(self, newQty):
            self.qty = newQty

        def setPrice(self, newPrice):
            self.price = newPrice

        #Display method
        def displayInfo(self):
            print "\tTitle: " + self.title
            print "\tQty: " + str(self.qty)
            print "\tPrice: %.2f", %self.price

我省略了一些完整的函数,但为了论证起见,假设在def changeQty(inventory), book 是一个对象列表:标题、数量、价格。

4

2 回答 2

0

您可能正在处理额外的空白,在这种情况下 .trim() 会有所帮助,但总的来说,对于字符串比较,我发现 .contains() 比 == 更有用。

就像上面 Moj 的例子,但是:

if title.lower().contains(reference.getTitle().lower().trim()):
    #do stuff
于 2013-04-26T21:52:01.380 回答
0

我想这应该可行:

if title.lower() !=reference.getTitle().lower():

    def getTitle(self):
        return self.title.lower() #or you can call this method when you want to do the comparison
于 2013-04-26T20:49:16.127 回答