所以我被困在我的項目的一部分。在這個項目中,我們必須讀庫文件到字典中創造的東西,如:python項目中的字典值
inventory = {author: "book title, quantity, price"}
,我已經做了。然後我們必須做一個功能
- 顯示圖書館,排序。
- 向作者添加一本書。
- 更改書的數量。
- 計算圖書館的總金額。
- 退出。
我被困在第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)
,本書是對象的列表:名稱,數量,價格。
你可以給你進入一個例子嗎? – Moj 2013-04-26 20:42:08
say inventoy = {「Dickens,Charles」:「Hard Times」,8,8.99} – 2013-04-26 20:48:44
檢查下面的代碼,如果我正確理解你的問題 – Moj 2013-04-26 20:50:47