python 添加/删除列表中的项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2496926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:46:58  来源:igfitidea点击:

add/remove items in a list

python

提问by Jam

I'm trying to create a player who can add and remove items from their inventory. I have everything working, I just have 1 small problem. Every time it prints the inventory, 'None' also appears. I've been messing with it to try and remove that, but no matter what I do, 'None' always appears in the program! I know I'm just missing something simple, but I can't figure it out for the life of me.

我正在尝试创建一个可以从他们的库存中添加和删除项目的玩家。我一切正常,我只有 1 个小问题。每次打印清单时,也会出现“无”。我一直在弄乱它以尝试删除它,但无论我做什么,“无”总是出现在程序中!我知道我只是错过了一些简单的东西,但我一生都无法弄清楚。

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.")

采纳答案by dmazzoni

The problem is this statement:

问题是这个语句:

print "Inventory:", player.inventory()

You're telling Python to print the value returned from player.inventory(). But your inventory() method just prints the inventory, it doesn't return anything - so the return value is implicitly None.

您告诉 Python 打印从 player.inventory() 返回的值。但是您的inventory() 方法只是打印库存,它不返回任何内容-因此返回值隐式为None。

You probably want to explicitly choose either this:

您可能希望明确选择以下任一选项:

print "Inventory:"
player.print_inventory()

Or alternatively you could have it return a string and do this:

或者,您可以让它返回一个字符串并执行以下操作:

print "Inventory:", player.inventory_as_str()

回答by Nope

Would you mind replacing the function:

你介意更换这个功能吗:

def inventory(self):
  for item in self.items:
      print item

with this:

有了这个:

def inventory(self):
    print self.items

and then call:

然后调用:

print "Inventory"
player.inventory()

Or you could have the function:

或者你可以拥有以下功能:

def print_inventory(self):
    print "Inventory:"
    for item in self.items:
        print item