Python 为什么这个if语句会导致语法错误

Python 为什么这个if语句会导致语法错误,python,syntax,Python,Syntax,我正在尝试设置一个elif语句,如果用户点击enter键,代码将继续,但是我会得到持续的语法错误 GTIN = 0 while True: try: GTIN = int(input("input your gtin-8 number:")) if len(str(GTIN)) == 8: break else: print("make sure the length of the barco

我正在尝试设置一个elif语句,如果用户点击enter键,代码将继续,但是我会得到持续的语法错误

GTIN = 0
while True:
    try:
        GTIN = int(input("input your gtin-8 number:"))
        if len(str(GTIN)) == 8:
            break
        else:
            print("make sure the length of the barcode is 8")
        elif:
            GTIN=(""):

您不能在
elif
之前使用
else
。另一个问题是,您必须在
中添加
除外
,然后重试

GTIN = 0
while True:
  GTIN = int(input("input your gtin-8 number:"))
  if len(str(GTIN)) == 8:
    print("OK: %s" % GTIN)
    break
  else:
    print("make sure the length of the barcode is 8")
编辑:您不需要elif。如果输入长度为8 OK,否则请重试

Edit2:不需要
尝试,除了
之外。

ps:print(“”)如果使用Python3,则顺序必须是
if
then(零或更多)
elif
然后最后
else
。另外,
elif
必须有一个条件,以同样的方式检查
如果
有。
else
必须是最后一个子句,和
elif
必须进行测试。您的代码中没有
除了
大小写。您希望代码生成什么?
尝试
需要一个
除了
最后
子句将生成
语法错误