2013-04-26 80 views
0

所以我被困在我的項目的一部分。在這個項目中,我們必須讀庫文件到字典中創造的東西,如:python項目中的字典值

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),本書是對象的列表:名稱,數量,價格。

+0

你可以給你進入一個例子嗎? – Moj 2013-04-26 20:42:08

+0

say inventoy = {「Dickens,Charles」:「Hard Times」,8,8.99} – 2013-04-26 20:48:44

+0

檢查下面的代碼,如果我正確理解你的問題 – Moj 2013-04-26 20:50:47

回答

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 
+0

這是行不通的。此外,圖書標題應該可以以任何方式輸入,例如title =「hArD TiMes」,但它應該最終看起來像title =「Hard Times」,以檢查圖書是否在庫存 – 2013-04-26 20:53:17

+0

以及使用時'str.lower()'將所有的字母轉換爲小寫字母,例如:「hArD TiMes」.lower()變成'難度時間'。你可以(我想你應該)在用戶輸入標題和庫存標題上對這種方法進行分類,然後檢查它們是否相等。 – Moj 2013-04-26 20:59:47

+0

這似乎也不工作,但我沒有看到代碼 – 2013-04-26 21:12:19

0

你可能會涉及額外的空白區域,在這種情況下.trim()可以幫助,但總體來說字符串比較,我發現。載有()更多有用比==。

上面一樣司法部的例子,但是:

if title.lower().contains(reference.getTitle().lower().trim()): 
    #do stuff