简体   繁体   中英

List returns None value instead of class object

In my role-playing party creator program, I am trying to have the user create a class object, adding the attributes, and storing it into a party-list index. However, by the time the player goes back to the main menu (the main() function that displays the party list), the slot still shows a None value. Here is my code:

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()

This is how the program runs so far:

[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

However, after you go back to main(), the list still displays as such:

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

What doesn't make sense is that the parameters in the functions should have followed a chain rule that will eventually lead to the party slot. I want it so that a slot index will have a class object stored in it rather than None. As far as I know, I might need to use global variables, but I haven't found out much after that. Any ways to fix this?

Edit: So I managed to fix the problem. I just put the check() function in the main(). It goes like this:

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])

There is no sens of "return" at the end of main. What you should want to do I think is editing the party list. Therefor instead of

return check(party[inp_1])

you should try

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

Make sure the check function returns a creature type, I'm not sure of that.

there is some really weird interaction you really should try to make class and methods for all of that. In your 4th elseif in the menu function, you shouldn't have to call main again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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