所以我被困在我项目的一部分上。在这个项目中,我们必须将库文件读入字典来创建类似的内容:
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)
, book 是一个对象列表:标题、数量、价格。