Python 2.7 (Python)if语句的返回值未分配给if语句外部的变量
控制台上打印的内容是:(假设Python 2.7 (Python)if语句的返回值未分配给if语句外部的变量,python-2.7,immutability,Python 2.7,Immutability,控制台上打印的内容是:(假设选项=“A”) 食肉动物 食肉动物 没有 这是否意味着饮食风格是不变的?如果是这样,我应该如何更改函数以将不同的值指定给饮食风格?您的参数作为值传递。分配新值不会对其产生任何影响。您可以像这样返回一个新字符串 def my_diet(event, style): if event == 1: choice = raw_input("""What type of animal are you? A) Carnivore
选项=“A”)
食肉动物
食肉动物
没有
这是否意味着饮食风格是不变的?如果是这样,我应该如何更改函数以将不同的值指定给饮食风格?您的参数作为值传递。分配新值不会对其产生任何影响。您可以像这样返回一个新字符串
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
查看此链接以了解更多信息。
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