Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.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代码中,在哪里编写最终的(else语句)?_Python_Python 3.x_If Statement - Fatal编程技术网

在这个python代码中,在哪里编写最终的(else语句)?

在这个python代码中,在哪里编写最终的(else语句)?,python,python-3.x,if-statement,Python,Python 3.x,If Statement,我是python新手,我喜欢尝试一些东西,所以我在(Python3.4.3)shell中编写了这些if和else语句,但我不知道在哪里编写最终的else语句,它导致所有4个值都为false 请注意: a=False b=False c=False d=False 这是代码的截图,因为这里的代码插入功能总是剪切我代码的很大一部分 您可以更简单地执行此操作,而无需大量

我是python新手,我喜欢尝试一些东西,所以我在(Python3.4.3)shell中编写了这些
if
else
语句,但我不知道在哪里编写最终的else语句,它导致所有4个值都为false

请注意:

a=False
b=False
c=False
d=False
这是代码的截图,因为这里的代码插入功能总是剪切我代码的很大一部分
您可以更简单地执行此操作,而无需大量
code

如果您有
4
boolean
变量
a
b
c
d
,则可以执行以下操作:

print("a is", a, "b is", b, "c is", c, "and d is", d)
它将
打印
订单中的某些内容:

a is False b is False c is False and d is False

以下是一些
示例
,以演示一些案例:

>>> a, b, c, d = True, True, True, True
>>> print("a is", a, "b is", b, "c is", c, "and d is", d)
a is True b is True c is True and d is True
>>> a, b, c, d = True, False, True, False
>>> print("a is", a, "b is", b, "c is", c, "and d is", d)
a is True b is False c is True and d is False
>>> a, b, c, d = False, False, True, True
>>> print("a is", a, "b is", b, "c is", c, "and d is", d)
a is False b is False c is True and d is True

啊,我现在明白了。您想知道为什么
shell
会给您一个
错误
。那很简单,因为你试图从一个
if
中执行
2
else``语句

if
语句的格式为:

if <condition>:
   code...
elif <condition>:
   code...
else:
   code...

如果
语句
,则不能从一个
语句中包含
2
else
子句!也许您想进一步缩进
以匹配不同的
语句
,但希望您能理解Python在这里抛出
错误的原因。

您的代码中有一些逻辑错误。例如,只要
a
false
b
为true,即使
c
和/或
d
false
,它都会达到“b为false,其余为true”。要做到这一点,您需要测试所有变量,直到
d
,无论您在
a
b
c
中发现了什么。如果您这样做,您将在
d
级别得到16个“叶子”,并且您将在这些叶子中编写所有
print
语句。下面是添加了额外分支的代码版本。“all false”部分在else/else/else/else分支结束,它应该在那里

if a:
    if b:
        if c:
            if d:
                print("a,b,c,d are true")
            else:
                print("d is false; a,b,c are true")
        else:  # c false
            # more detail added below
            if d:
                print("c is false; a,b,d are true")
            else:
                print("c,d are false; a,b are true")
    else:  # b false
        # more detail added below
        if c:
            if d:
                print("b is false; a,c,d are true")
            else:
                print("b,d are false; a,c are true")
        else:  # c false
            if d:
                print("b,c are false; a,d are true")
            else:
                print("b,c,d are false; a is true")
else:  # a false
    if b:
        if c:
            if d:
                print("a is false; b,c,d are true")
            else:
                print("a,d are false; b,c are true")
        else:  # c false
            # more detail added below
            if d:
                print("a,c are false; b,d are true")
            else:
                print("a,c,d are false; b is true")
    else:  # b false
        # more detail added below
        if c:
            if d:
                print("a,b are false; c,d are true")
            else:
                print("a,b,d are false; c is true")
        else:  # c false
            if d:
                print("a,b,c are false; d is true")
            else:
                print("a,b,c,d are false")
关于您的错误--您的代码如下所示,这是无效的:

if a:
    # something
else:
    # something else
else:
    print("a,b,c,d are false")

专业提示:永远不要直接在pythonshell中输入那么多代码。将代码存储在一个文件中,并在该文件上运行Python。感谢您的回答,但我不想只写a是False b是False c是False,d是False我想要一个能检查这些值的所有可能性并给出正确输出的代码,谢谢again@peter我认为你没有抓住重点,这样做是因为
打印
中的逗号将
变量
字符串
分开,这意味着它们的值将发生变化。我会添加更多的例子来演示我理解你,兄弟,但这些值只是一个例子,我想知道为什么当我试图把最后一个else语句放在这里时,我会出错,再次感谢你的关心mate@peter我在代码的底部添加了一个解释。如果有帮助的话,请随时投票并接受:)你是一位生命救世主,兄弟,我非常感谢你。这真的非常有用。在您的代码中,您检查了这些值的每一种可能性,我现在将尝试自己编写,谢谢专业人士。
if a:
    # something
else:
    # something else
else:
    print("a,b,c,d are false")