在python编程中的OOP,我不断收到有关定义main()的错误消息

时间:2014-07-21 00:44:34

标签: python

def main():
    name = input("What would you like to name your character?")
    print("Your character has been created!")
    play = Player(name)



class Player():
    """A game player"""



    def __init__(self, name, items, max_items = 7):
        self.__name = name
        self.__items = []
        self.__max_items = 7


    def __str__(self):
        return self.__name


    def charInfo(self):
        print("This is your character's name:", self.__name)

    def inventory(self):
        if len(self.__items) > 7:
            print ("Your iventory is:")
            print (self.__items)
        else:
            print("There's nothing in your inventory.")

    def take(self, add_item):
        if self.__items <= self.__max_items:
            self.__items.append(add_item)
        else:
            print("You already have the max items in your inventory,sorry")

    def drop(self, drop_item):
        if drop_item not in self.__items:
            print("That item isn't in your inventory.")
        else:
            self.__items.remove(drop_item)

choice = None
while choice != 0:

    print(""" Character Menu
            0 - Quit
            1 - Character Information
            2 - Inventory
            3 - Add an item
            4 - Drop an item""")

    choice = input("What would you like to do?")

    #Exit
    if choice == "0":
        print("See you later!")

    #Character Information
    elif choice == "1":
        play.charInfo()

    #Inventory
    elif choice == "2":
        play.inventory()

    #Add an item
    elif choice == "3":
        add_item = input("What would you like to add to your inventory?")
        add_item = add_item.lower()
        play.take(add_item)

    #Drop an item
    elif choice == "4":
        drop_item = input("What item would you like to drop?")
        drop_item = drop_item.lower()
        play.drop(drop_item)

main()
input("\nPress enter to exit.")

我一直收到此错误消息,但我不理解原因。它看起来好像我已经定义了main()。我试图看看是否有人可以帮助我理解为什么我收到此错误消息。我是python的新手

2 个答案:

答案 0 :(得分:3)

您在main类中定义了Player,因此它仅作为Player对象上的方法存在(它将不可调用,因为它不带参数)。

在你可以打电话之前,你需要将它提升到最高级别。

另外:不要将self.__foo用于属性名称。它不会使它们变得私密,阅读起来真的很奇怪,而且它主要是让你感到悲伤。如果必须,请使用self._foo,但除非您有充分的理由隐藏您的州,否则请使用self.foo

答案 1 :(得分:0)

您应该在课程main()之外写Player() 修改:
您的代码将以这种方式工作:

def main():
    name = input("What would you like to name your character?")
    print("Your character has been created!")
    play = Player(name)
    choice = None
    while choice != "0":
        print(""" Character Menu
            0 - Quit
            1 - Character Information
            2 - Inventory
            3 - Add an item
            4 - Drop an item""")
        choice = input("What would you like to do?")
        #Exit
        if choice == "0":
            print("See you later!")
        #Character Information
        elif choice == "1":
            play.charInfo()
        #Inventory
        elif choice == "2":
            play.inventory()
        #Add an item
        elif choice == "3":
            add_item = input("What would you like to add to your inventory?")
            add_item = add_item.lower()
            play.take(add_item)
        #Drop an item
        elif choice == "4":
            drop_item = input("What item would you like to drop?")
            drop_item = drop_item.lower()
            play.drop(drop_item)


class Player():
    """A game player"""



    def __init__(self, name):
        self.__name = name
        self.__items = []
        self.__max_items = 7


    def __str__(self):
        return self.__name


    def charInfo(self):
        print("This is your character's name:", self.__name)

    def inventory(self):
        if len(self.__items) > 7:
            print ("Your iventory is:")
            print (self.__items)
        else:
            print("There's nothing in your inventory.")

    def take(self, add_item):
        if len(self.__items) <= self.__max_items:
            self.__items.append(add_item)
        else:
            print("You already have the max items in your inventory,sorry")

    def drop(self, drop_item):
        if drop_item not in self.__items:
            print("That item isn't in your inventory.")
        else:
            self.__items.remove(drop_item)

main()

input("\nPress enter to exit.")