Python说elif语句的语法错误

时间:2013-10-28 03:16:11

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

flag=False
if color1=="blue":
    color2=input("Do you want to mix red or yellow with blue?")
    color2=color2.lower()
    while flag==False:
        if color2=="red" or color2=="yellow":
            flag = True
        else:
            color2=input("That is not a valid choice. Enter either red or yellow to mix\ with", color1)
    if color2=="red":
        print("The color you made is purple")
    else:
        print("The color you made is green")
flag=False
elif color1=="red":
    color2=input("Do you want to mix blue or yellow with red?")
    color2=color2.lower()
    while flag==False:
        if color2=="blue" or color2=="yellow":
            flag = True
        else:
            color2=input("That is not a valid choice. Enter either blue or yellow to\ mix with", color1)

我遇到了一个问题,就是说“elif color1 ==”red“:”的行中间的一个elif语句。只是为了得到一些上下文,这是一个简单的程序,将混合三种基色。

1 个答案:

答案 0 :(得分:3)

假设程序中的缩进与问题中显示的缩进相同,则flag=False之前的行elif需要缩进,否则解析器会认为你已经缩进当你实际上没有时,离开if区块。