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

编程入门 行业动态 更新时间:2024-10-25 00:28:24
本文介绍了为什么这个 if 语句会导致语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试设置一个 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屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 13:45:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1410186.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语句   语法错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!