Home > database >  The local variable is referenced before the assigment
The local variable is referenced before the assigment

Time:03-24

The error i am recieving is UnboundLocalError: local variable 'this_mammal' referenced before assignment

I have been practicing this stuff for possibly like a few days now but this stumped me. I've looked around for indents and everywhere i have this_mammal placed but it seems okay. I couldnt include the Mammal superclass and animal subclasses because i didnt have enough text to include all the code. Please let me know if thats necessary to include! I can try.

def select_which_mammal():
    print("Welcome to the Mammal Inventory Management System!")
    print()
    print("1. Cow")
    print("2. Cat")
    print("3. Dog")
    print("4. Human")
    print("5. Platypus")
    print("6. Echidna")
    print("7. Bat")
    print()
    selection = int(input("Enter the number for the mammal you would like to add:"))

    while selection < 1 or selection > 7:
        print("ERROR: Selections must be 1-7.")
        selection = int(input("Enter the number for the mammal you would like to add:"))

def create_the_mammal(mammal_type):
    
    if mammal_type == 1: this_mammal = Mammal('Cow', 4, 3)
    
    elif mammal_type == 2: this_mammal = Mammal('Cat', 4, 9)

    elif mammal_type == 3: this_mammal = Mammal('Dog', 4, 6)

    elif mammal_type == 4: this_mammal = mammal("Human", 2, 14)
    
    elif mammal_type == 5: this_mammal = mammal("Platypus", 4, 3)

    elif mammal_type == 6: this_mammal = mammal("Echidna", 4, 3)

    elif mammal_type == 7: this_mammal = mammal("Bat", 2, 1)

    print("\nYour", this_mammal.get_name(), "has been added to the system.\n")
    
    return this_mammal

def choose_an_action(this_mammal): 
    print("Next, please choose an action for your ", this_mammal.get_name()   ".")
    print()
    print("1. Display the mammal's status")
    print("2. Hear the mammal's sound")
    print("3. Go out for a walk with your mammal")
    print("4. Breed your mammal")
    print()
    action = int(input("Enter the number for the action:"))

    while action < 1 or action > 4:
        print("ERROR: Selections must be 1-4.")
        action = input(int("Enter the number for the action:"))

    if action == 1: this_mammal.show_self()

    elif action == 2: this_mammal.make_sound()

    elif action == 3: this_mammal.ambulate()

    elif action == 4: this_mammal.reproduce()

def main():

    new_mammal = select_which_mammal()

    this_mammal = create_the_mammal(new_mammal)

    choose_an_action(this_mammal)

main()

CodePudding user response:

you have to return selection at the end of the select_which_mammal function.

  • Related