0
def my_diet(event, style):
    if event == 1:
        choice = raw_input("""What type of animal are you?
        A) Carnivore
        B) Herbivore
        C) Omnivore
        """)
        if choice == "A":
            style = "Carnivore"
            print style
        elif choice == "B":
            style = "Herbivore"
        elif choice == "C":
            style = "Omnivore"
        else:
            style = "not-sure-yet"
        print style
    else:
        pass
    return style
    print style

eating_style = None
my_diet(1, eating_style)
print eating_style

What was printed on the console was: (assuming choice = "A")

Carnivore

Carnivore

None

Does this mean that eating_style is immutable? If so, how should I change the function to assign a different value to eating_style?

user94216
  • 45
  • 1
  • 4

1 Answers1

1

Your argument is passed as a value. Assigning a new value won't have any effect on it. You can just return a new string like this.

def my_diet(event):
    if event == 1:
        choice = raw_input("""What type of animal are you?
        A) Carnivore
        B) Herbivore
        C) Omnivore
        """)
        style = ""
        if choice == "A":
            style = "Carnivore"
        elif choice == "B":
            style = "Herbivore"
        elif choice == "C":
            style = "Omnivore"
        else:
            style = "not-sure-yet"
    else:
        pass
    return style

eating_style = my_diet(1)
print eating_style

Check this link for more information. How do I pass a variable by reference?

Community
  • 1
  • 1
Will_Panda
  • 534
  • 10
  • 26