Python项目:创建一个程序来跟踪向导可以携带的项目

时间:2018-07-02 21:19:36

标签: python

show不起作用,并且不会显示我的任何物品

在我的代码的第一个文件中,我具有以下内容:

items.py:

list(inventory_list):
inventory = ["a wooden staff", "a wizard hat", "a cloak of invisibility",

"some elven bread", "an unknown potion", "a scroll of uncursing",

"a scroll of invisibility", "a crossbow", "a wizard's cloak"]
item = inventory.pop()
item = inventory.pop(1)
item = inventory.pop(2)
item = inventory.pop(3)
item = inventory.pop(4)
item = invnetory.pop(5)
item = inventory.pop(6)
item = inventory.pop(7)
item = inventory.pop(8)

在我的另一个文件中,它是main.py文件,看起来像这样。

import random
import items as i
inventory_list = 0

def list(inventory_list):
    inventory = ["a wooden staff", "a wizard hat", "a cloak of invisibility",

    "some elven bread", "an unknown potion", "a scroll of uncursing",

    "a scroll of invisibility", "a crossbow", "a wizard's cloak"]
    item = inventory.pop()
    item = inventory.pop(1)
    item = inventory.pop(2)
    item = inventory.pop(3)
    item = inventory.pop(4)



def display_menu(inventory_list):
    random.shuffle(inventory_list)
    print("The Wizard Inventory Program")
    print()
    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")

def show(inventory_list):
    i = 1
    for item in inventory_list:
        print(str(i) + ". " + item)
        i += 1
    print()

def grab(inventory_list):
    item = input("Name: ")
    inventory_list.append(item)
    print(item + " was added.\n")

def drop(inventory_list):
    number = int(input("Number: "))
    if number < 1 or number > len(inventory_list):
        print("Invalid item number.\n")
    else:
        number = inventory_list.pop(number-1)
        print(item + " was deleted.\n")

def edit(inventory_list):
    number = int(input("Number: "))
    if number < 1 or number > len(inventory_list):
        print ("Invalid item number.\n")
    else:
        number = inventory_list.pop(input())
        print( item + "was edited to.\n")

def main():
    inventory_list = ["a wooden staff", "a wizard hat", "a cloak of invisibility",

    "some elven bread", "an unknown potion", "a scroll of uncursing",

    "a scroll of invisibility", "a crossbow", "a wizard's cloak"]
    display_menu(inventory_list)
    while True:
        command = input("Command: ")
        if command.lower() == "show":
            list(inventory_list)
        elif command.lower() == "grab":
            grab(inventory_list)
        elif command.lower() == "drop":
            drop(inventory_list)
        elif command.lower() == "exit":
            break
        else:
            print("Not a valid command. Please try again.\n")
    print("Bye!")

if __name__== "__main__":
    main()

另一个问题是我需要有单独的文件,还是可以全部放入一个文件中?

1 个答案:

答案 0 :(得分:0)

我认为这更符合您的需求。另外,我会为您提供帮助,因为我可以从您的代码中看到您正在苦苦挣扎的几个地方,但是请查看有关向SO发布问题的规则,因为如上所述,这不适合个人资料。并且还要检查我在做什么,并尝试理解原因。

import random
inventory_list = []

def display_menu(inventory_list):
    random.shuffle(inventory_list)
    print("The Wizard Inventory Program")
    print()
    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")

def invalid_number(num):
    try:
        x = inventory_list[num]
        return False
    except IndexError:
        return True

def show(inventory_list):
    for i, item in enumerate(inventory_list):
        print("{}. {}".format(i, item))
    print()

def grab(inventory_list):
    item = input("Name: ")
    inventory_list.append(item)
    print(item + " was added.\n")

def drop(inventory_list):
    number = int(input("Number: "))
    if invalid_number(number):
        print("Invalid item number.\n")
    else:
        orig_inp = inventory_list[number]
        del inventory_list[number]
        print("'{}' was deleted.\n".format(orig_inp))

def edit(inventory_list):
    number = int(input("Number: "))
    if invalid_number(number):
        print ("Invalid item number.\n")
    else:
        orig_inp = inventory_list[number]
        new_inp = input("What would you like to call '{}' instead? ".format(orig_inp))
        inventory_list[number] = new_inp
        print("'{}' was edited to '{}'.\n".format(orig_inp, new_inp))

def main():
    inventory_list = ["a wooden staff", "a wizard hat", "a cloak of invisibility",

    "some elven bread", "an unknown potion", "a scroll of uncursing",

    "a scroll of invisibility", "a crossbow", "a wizard's cloak"]
    display_menu(inventory_list)
    while True:
        command = input("Command: ").lower()
        if command == "show":
            show(inventory_list)
        elif command == "grab":
            grab(inventory_list)
        elif command == "drop":
            drop(inventory_list)
        elif command == "edit":
            edit(inventory_list)
        elif command == "exit":
            break
        else:
            print("Not a valid command. Please try again.\n")
    print("Bye!")

if __name__== "__main__":
    main()