为什么这个 if 语句会导致语法错误 [英] Why does this if statement cause a syntax error

查看:44
本文介绍了为什么这个 if 语句会导致语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个 elif 语句,如果用户按下回车键,代码将继续,但是我不断收到语法错误

I am trying to set up an elif statement , where if the user hits the enter key the code will continue , however i am getting constant syntax errors

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.另一个问题,您必须在 try 中添加 except.

You can't use else before elif. The other problem, you must add except in your try.

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,否则再做一次.

You don't need the elif. If input length is 8 OK, else do again.

Edit2:也不需要try except.ps: print("") 如果你使用 python 3

这篇关于为什么这个 if 语句会导致语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆