【问题标题】:python 3 list homework has me stumpedpython 3 list 作业让我很难过
【发布时间】:2017-03-01 03:36:13
【问题描述】:

好的,所以我应该创建一个向导清单程序,列出向导当前拥有的物品。他最多可以拿 4 件物品,从 3 件开始。我的功能似乎是问题所在,但我不确定我在书中做错了什么。

我的显示功能就像一个魅力......我的编辑不是编辑......是的。我只是把这个贴出来,希望你们能告诉我我做错了什么。感谢您提前提供的所有帮助。 (不,我没有要求任何人做我的作业......但也许只是指出我正确的方向)

print ("The Wizard Inventory program")

inventory = ["orb", "staff", "spellbook"]

def opening_menu():
    print("Command Menu")
    print("show - Show all items")
    print("grab - Grab an item")
    print("edit - Edit an item")
    print("drop - Drop an item")
    print("exit - Exit program")
    print()

def show_it():
    inventory = ["orb", "staff", "spellbook"]
    for item in inventory:
        print(item)

def grab_it():
    print("which item would you like to add??" )
    if (item -1) in range (master_inventory_list):
        while yes:
            grab_it (inventory, " ")

def edit_it():
    inventory = ["orb", "staff", "spellbook", "hat", "potion", "robe"]
    item = input("What item are you looking for? ")
    if item in inventory:
        inventory.remove(item)

def drop_it():
    inventory = ["orb", "staff", "spellbook", "hat", "potion", "robe"]
    item = input("What item would you like to drop? ")
    for item in inventory:
        inventory.pop()

def exit_it():
    choice = y
    while choice.lower() == "y":
        print("Do you want to stay inside your inventory? (y/n): ")
        print()
        print("Goodbye!!")

def master_inventory_list():
    master_inventory_list = ["orb", "staff", "spellbook", "hat", "potion", "robe"]

opening_menu()

while True:
    command = input("Command: ")
    if command.lower() == "show":
        show_it()
    elif command.lower() == "grab":
        grab_it()
    elif command.lower() == "edit":
        edit_it()
    elif command.lower() == "drop":
        drop_it()
    elif command.lower() == "exit":
        break
    else:
        print("Not a valid choice! Please choose again. \n")


print("Goodbye!")

if __name__ == "__main__":
    main()

【问题讨论】:

    标签: list function python-3.x


    【解决方案1】:

    你说的是作业,所以我假设你在过去一个月左右才开始学习。

    看一眼,可能是缺少退货单/全球库存/什么反映了你的库存变化?

    定义编辑_it(): 库存= [“orb”,“staff”,“spellbook”,“hat”,“potion”,“robe”] item = input("你在找什么项目?") 如果物品在库存中: 库存.删除(项目)

    在函数(如 edit_it)中,所有变量要么是函数的参数,要么是在函数内创建的,要么是全局变量。如果您希望您的库存反映您的更改,则需要将其输出到退货声明中。退货声明将为您提供您的清单/库存,但您可以自行选择清单。您可以将返回列表的值分配给另一个变量。

    但是,因为你在函数内部定义了inventory,所以每次调用函数时,都会做这样的声明/赋值:

    inventory = [“orb”、“staff”、“spellbook”、“hat”、“potion”、“robe”]

    实际上,您永远不会超过 5 个项目。因此,除了返回函数之外,您可能还想在每次想要编辑它时将库存作为参数传递给您的 edit_it 函数。举个例子:

    def example(inventory, itemToRemove):
        inventory.remove(itemToRemove)
        return inventory
    
    newThing = example(oldThing, theUserInput)
    
    newerThing = example(newThing, theNewUserInput)
    

    (顺便说一句,这些函数名称很糟糕,但我认为它对我所了解的一般结构给出了一个不错的总结。关于效率和清晰度还有很多话要说,但是嗯。)

    另外,如果你不介意我问,为什么你有 master_inventory_list() 原样?如果它是一个您可以在整个程序中使用的值,那么有一个更好的方法(这并不是函数的真正使用方式)。

    【讨论】:

    • 我猜主库存清单真的适合我吗?我想列出一些能显示尽可能多的项目的东西。是的,我最近才开始学习这些东西。
    • 顺便感谢您的评论!我还没有机会阅读完整的帖子(早上的生活义务),但在略读之后,你的建议会有所帮助!所以谢谢你。 :)
    • 啊,明白了。是的,你在函数中拥有 master_inventory_list 的方式,它似乎被用作普通变量。没有什么不好的事情真的会发生,但一般也不会发生任何事情。因此,如果您的教练因(看起来像)次要原因而扣分,那么按照现在的功能方式,它可能会合格。
    • 没问题!我希望它对您有所帮助,并随时询问您是否需要澄清。 Python 是一种非常有趣且有益的学习编程方式。
    • 你认为你可以给我展示另一个编辑或删除/添加的例子吗?我不断收到回溯错误,我不知道为什么.... def edit_it(inventory): item = input("What item are you looking for?") if item in inventory: inventory.remove(item) return inventory
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    相关资源
    最近更新 更多