繁体   English   中英

列表返回 None 值而不是 class object

[英]List returns None value instead of class object

在我的角色扮演派对创建程序中,我试图让用户创建一个 class object,添加属性,并将其存储到派对列表索引中。 但是,当玩家返回主菜单(显示派对列表的 main() function)时,该插槽仍显示 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

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

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

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

编辑:所以我设法解决了这个问题。 我只是将 check() function 放在 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])

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

return check(party[inp_1])

你应该试试

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

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

有一些非常奇怪的交互,你真的应该尝试制作 class 和所有这些的方法。 在菜单 function 的第 4 个 elseif 中,您不必再次调用 main。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM