首页 > 解决方案 > 我不断收到无效的语法,但我找不到它

问题描述

if (x == True):
     if (beverage_bought == True):
        if (fries_bought == True):
            print("You bought: " + x + " x, " + beverage + " drink, " + fries + " fries, and " + 
ketchup_string + " ketchup packets.")
            total = total - 1
        else:
            print ("You bought: " + x, " x, " + beverage + " drink, no fries and " + ketchup_string + " ketchup packets.")
        elif (fries_bought == True):
            print ("You bought: " + x + " x, nothing to drink, " + fries + " fries, and " + ketchup_string + " ketchup packets.")
        else:
            print ("You bought: " + x, " x, nothing to drink, no fries, and " + ketchup_string + " 
ketchup packets.")

print (total)

它一直说我的 elif 行的语法无效

标签: python

解决方案


elseif之后不能使用elseelse必须是链的最后一个:if> elif> elif> else

您应该删除第二个的缩进elif以使其与第一个对齐if

if x:
    if beverage_bought:
        if fries_bought:
            print("You bought: " + x + " x, " + beverage + " drink, " + fries + " fries, and " + ketchup_string + " ketchup packets.")
            total = total - 1
        else:
            print("You bought: " + x, " x, " + beverage + " drink, no fries and " + ketchup_string + " ketchup packets.")

    elif fries_bought:
        print("You bought: " + x + " x, nothing to drink, " + fries + " fries, and " + ketchup_string + " ketchup packets.")
    else:
        print("You bought: " + x, " x, nothing to drink, no fries, and " + ketchup_string + " ketchup packets.")

print(total)

推荐阅读