python 3 中的无效语法(elif 语句)

叶特

当我尝试运行此代码时,我在第一个 elif 语句中收到一个名为“无效语法”的错误。我一直在研究,但找不到它不起作用的原因。我很确定我使用的是最新版本的 python (3.8.2)。

def likes(names):
  if (len(names) >= 4):
      print("{}, {} and {} others like this".format(names[0], names[1], len(names)-2)
  elif (len(names) == 3):
      print("{}, {} and {} like this".format(names[0], names[1], names[2])
  elif (len(names) == 2):
      print("{} and {} like this".format(names[0], names[1])
  elif (len(names) == 1):
      print("{} likes this".format(names[0])
  else:
      print("no one likes this")
awarrier99

print除了最后一个之外,您的所有语句都缺少匹配括号,它应该是:

def likes(names):
  if (len(names) >= 4):
      print("{}, {} and {} others like this".format(names[0], names[1], len(names)-2))
  elif (len(names) == 3):
      print("{}, {} and {} like this".format(names[0], names[1], names[2]))
  elif (len(names) == 2):
      print("{} and {} like this".format(names[0], names[1]))
  elif (len(names) == 1):
      print("{} likes this".format(names[0]))
  else:
      print("no one likes this")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章