Python 为什么这个if语句会导致语法错误
我正在尝试设置一个elif语句,如果用户点击enter键,代码将继续,但是我会得到持续的语法错误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
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
必须进行测试。您的代码中没有除了
大小写。您希望代码生成什么?尝试
需要一个除了
或最后
子句将生成语法错误
。