【问题标题】:List returns None value instead of class object列表返回 None 值而不是类对象
【发布时间】:2021-07-05 13:40:23
【问题描述】:

在我的角色扮演派对创建程序中,我试图让用户创建一个类对象,添加属性,并将其存储到派对列表索引中。但是,当玩家返回主菜单(显示派对列表的 main() 函数)时,插槽仍然显示 None 值。这是我的代码:

class Creature:
    def __init__(self):
        self.name = None
        self.feet = None
        self.inches = None
        self.weight = None
        self.gender = None

    def getName(self):
        return "Name: {}".format(self.name)

    def setName(self, name):
         self.name = name

    def getHeight(self):
        return "Height: {} ft. {} in.".format(self.feet, self.inches)

    def setFeet(self, feet):
        self.feet = feet

    def setInches(self, inches):
        self.inches = inches

    def getWeight(self):
        return "Weight: {} lbs.".format(self.weight)

    def setWeight(self, weight):
        self.weight = weight

    def getGender(self):
        return "Gender: {}".format(self.gender)

    def setGender(self, index):
        genders = ['Male', 'Female', 'Others']
        if int(index) == 1:
            self.gender = genders[0]
        elif int(index) == 2:
            self.gender = genders[1]
        elif int(index) == 3:
            self.gender = genders[2]

class Dragon(Creature):
    pass

class Mermaid(Creature):
    pass

class Fairy(Creature):
    pass

class Vampire(Creature):
    pass


#allows the user to change attributes of creature
def changeAttributes(creature):
    value = input("Pick an attribute to change: 1) name   2) height   3) weight   4) gender   5) save")
    if int(value) == 1:
        creature.setName(input("Enter a name: "))
        return changeAttributes(creature)
    elif int(value) == 2:
        creature.setFeet(input("Enter a foot value: "))
        creature.setInches(input("Enter an inch value: "))
        return changeAttributes(creature)
    elif int(value) == 3:
        creature.setWeight(input("Enter a value in pounds: "))
        return changeAttributes(creature)
    elif int(value) == 4:
        creature.setGender(input("Enter a value to set gender; 1 = male, 2 = female, 3 = others: "))
        return changeAttributes(creature)
    elif int(value) == 5:
        confirm = input("Save?  1) yes  2) no")
        if int(confirm) == 1:
            print('Saving...')
            return menu(creature)
        else:
            return changeAttributes(creature)
    else:
        print("Not a valid input, please try again.")
        return changeAttributes(creature)

#prints the attributes of the creature
def showAttributes(creature):
    print(creature.getName())
    print(creature.getHeight())
    print(creature.getWeight())
    print(creature.getGender())
    menu(creature)

def Delete(creature):
    a = input("Are you sure?  1) yes   2) no  ")
    if int(a) == 1:
        print("Deleting...")
        creature = None
        return main()
    elif int(a) == 2:
        print("Cancelled")
        return menu(creature)

#checks to see if slot is empty or has a creature object; if empty, create a creature, otherwise go to creature menu
def menu(creature):
    value = input("Select an option  1) Show Attributes   2) Change Attributes  3) Delete   4) Back")
    if int(value) == 1:
        return showAttributes(creature)
        return menu(creature)
    elif int(value) == 2:
        return changeAttributes(creature)
        return menu(creature)
    elif int(value) == 3:
        return Delete(creature)
    elif int(value) == 4:
        return main()

#checks if slot is empty, if empty, choose a creature subclass and change attributes, else takes user directly to change attribute menu
def check(slot):
    if slot == None:
        a = input('Choose a creature: 1) Dragon   2) Fairy   3) Mermaid   4) Vampire')
        if int(a) == 1:
            slot = Dragon()
        elif int(a) == 2:
            slot = Fairy()
        elif int(a) == 3:
            slot = Mermaid()
        elif int(a) == 4:
            slot = Vampire()
        return changeAttributes(slot)
    else:
        return menu(slot)

#user select a slot; note that since development has not finished, you can only change slot 1
def main():
    global party
    print(party)
    inp = input("Select a slot: ")
    inp_1 = int(inp) - 1
    if int(inp) > 0 and int(inp) < 6:
        print("Slot {} selected!".format(int(inp)))
        return check(party[inp_1])

party = [None, None, None, None, None]

main()

到目前为止,程序是这样运行的:

[None, None, None, None, None]
Select a slot:
#User inputs 1
Slot 1 selected!
Choose a creature: 1) Dragon   2) Fairy   3) Mermaid   4) Vampire
#User inputs 1
Pick an attribute to change: 1) name   2) height   3) weight   4) gender   5) save
#User inputs 1
Enter a name: *Name*
Pick an attribute to change: 1) name   2) height   3) weight   4) gender   5) save
#User inputs 5
Save?  1) yes  2) no
#User inputs 1
Saving...
Select an option  1) Show Attributes   2) Change Attributes  3) Delete   4) Back
#User inputs 4

但是,在你返回 main() 之后,列表仍然显示如下:

[None, None, None, None, None]
Select a slot: 

没有意义的是,函数中的参数应该遵循链式规则,最终将导致派对槽。我想要它,以便插槽索引将存储一个类对象而不是无。据我所知,我可能需要使用全局变量,但在那之后我没有发现太多。有什么办法可以解决这个问题?

编辑:所以我设法解决了这个问题。我只是将 check() 函数放在 main() 中。它是这样的:

def main():
    print(party)
    inp = input("Select a slot: ")
    inp_1 = int(inp) - 1
    if int(inp) > 0 and int(inp) < 6:
        print("Slot {} selected!".format(int(inp)))
        if party[inp_1] == None:
            a = input('Choose a creature: 1) Dragon   2) Fairy   3) Mermaid   4) Vampire')
            if int(a) == 1:
                slot = Dragon()
            elif int(a) == 2:
                slot = Fairy()
            elif int(a) == 3:
                slot = Mermaid()
            elif int(a) == 4:
                slot = Vampire()
            party[inp_1] = slot
            return changeAttributes(party[inp_1])
        else:
            return menu(party[inp_1])

【问题讨论】:

    标签: python list class object nonetype


    【解决方案1】:

    main 结尾没有“返回”的感觉。我认为您应该做的是编辑派对列表。因此,而不是

    return check(party[inp_1])
    

    你应该试试

    party[inp_1] = check(party[inp_1])
    

    确保检查函数返回一个生物类型,我不确定。

    有一些非常奇怪的交互,你真的应该尝试为所有这些创建类和方法。在菜单功能的第 4 个 elseif 中,您不必再次调用 main。

    【讨论】:

    • 我已经做到了;插槽仍然显示 None。
    • 好的,如果我从 menu() 中删除第 4 个 elseif,那么如何返回主派对列表菜单?
    • 从技术上讲,在菜单功能中,只有 2 个选项可以更改对象,对吧?更改属性并删除。正确的?因此,您应该期望“显示属性”进入函数并且不返回任何内容好吗?或者最多返回未更改的实例。对于更改属性,它看起来还可以。当你从菜单返回时,你会在“检查”结束时结束,这会让你回到主循环。您应该将此视为阶段,例如,如果您在返回后(或在函数结束时)更深入,您将从原来的位置返回。
    • 我已经设法通过删除 check() 并将其全部放入 main() 来解决问题
    猜你喜欢
    • 2022-12-22
    • 1970-01-01
    • 2019-10-13
    • 2021-09-18
    • 2015-06-26
    • 1970-01-01
    • 2011-10-19
    • 2019-06-12
    • 2016-08-08
    相关资源
    最近更新 更多