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

2024-06-17 12:03:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试设置一个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=(""):

Tags: the代码用户trueinputyour语句int
1条回答
网友
1楼 · 发布于 2024-06-17 12:03:56

不能在elif之前使用else。另一个问题是,必须在try中添加except。你知道吗

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,则可以,否则请重试。你知道吗

Edit2:也不需要try exceptps:如果您使用python 3

相关问题 更多 >