我正在嘗試創建一個可以從其庫存中添加和刪除項目的玩家。我有一切工作,我只有一個小問題。每次打印庫存時,都會顯示「無」。我一直在試圖去除它,但無論我做什麼,都會在程序中出現「無」!我知道我只是想念一件簡單的事情,但我無法想象出我的一生。添加/刪除列表中的項目
class Player(object):
def __init__(self, name, max_items, items):
self.name=name
self.max_items=max_items
self.items=items
def inventory(self):
for item in self.items:
print item
def take(self, new_item):
if len(self.items)<self.max_items:
self.items.append(new_item)
else:
print "You can't carry any more items!"
def drop(self, old_item):
if old_item in self.items:
self.items.remove(old_item)
else:
print "You don't have that item."
def main():
player=Player("Jimmy", 5, ['sword', 'shield', 'ax'])
print "Max items:", player.max_items
print "Inventory:", player.inventory()
choice=None
while choice!="0":
print \
"""
Inventory Man
0 - Quit
1 - Add an item to inventory
2 - Remove an item from inventory
"""
choice=raw_input("Choice: ")
print
if choice=="0":
print "Good-bye."
elif choice=="1":
new_item=raw_input("What item would you like to add to your inventory?")
player.take(new_item)
print "Inventory:", player.inventory()
elif choice=="2":
old_item=raw_input("What item would you like to remove from your inventory?")
player.drop(old_item)
print "Inventory:", player.inventory()
else:
print "\nSorry, but", choice, "isn't a valid choice."
main()
raw_input("Press enter to exit.")
您可以發佈您的輸入和輸出的樣本? – 2010-03-23 01:25:05