在这个python代码中写下final(else语句)的位置?

时间:2017-10-28 21:28:35

标签: python python-3.x if-statement

我是学习python的新手,我喜欢尝试所以我在(python 3.4.3)shell中编写了这些ifelse语句,但我不知道在哪里写导致所有4个值的最终else语句都是错误的。

请注意:

a=False
b=False
c=False
d=False

这是代码的屏幕截图,因为这里的代码插入功能总是占用我代码的很大一部分 screenshot

2 个答案:

答案 0 :(得分:1)

如果没有if-else大量 code块,你可以更简单地做到这一点。

如果您有4 boolean variablesabcd,那么您可以执行以下内容:

print("a is", a, "b is", b, "c is", c, "and d is", d)

print订单中的某些内容:

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

以下是一些examples来展示一些案例:

>>> 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给您error的原因。好吧,这仅仅是因为你试图从一个2开始else``statements if

if statement的格式为:

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

其中elifelse 可选

您在该屏幕截图中使用的syntax是:

if a:
   code...
else:
   code...
else:
ERROR

您只能从一个2 else获得if statement条款!也许你打算indent进一步匹配不同的statement,但希望你理解为什么Python在这里抛出error

答案 1 :(得分:0)

您的代码中存在一些逻辑错误。例如,即使a和/或{{1},任何时候False bc都为真,它将达到“b为假,其余为真”是d。要做到这一点,无论您为Falseda找到什么,都需要将所有变量一直测试到b。如果你这样做,你最终会得到c级别的16个'叶子',你将在这些叶子中写下所有d个语句。下面是您添加了额外分支的代码版本。 “all false”部分最终出现在else / else / else / else分支处,应该是。

print

关于您的错误 - 您的代码如下,无效:

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")