Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 (Python)if语句的返回值未分配给if语句外部的变量_Python 2.7_Immutability - Fatal编程技术网

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