簡體   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