【问题标题】:Syntax error with 'if or' in python. Not sure what's wrong with the code [closed]python 中“if or”的语法错误。不知道代码有什么问题[关闭]
【发布时间】:2014-12-03 18:51:20
【问题描述】:
for i in range(0,len(qList)):
    response=(input((stringify(aList[i])))
    if (response==cList[i]) or ((" "+response) in aList[i]):

其中 cList 是字符串列表,aList 是字符串列表列表。 stringify 是一个帮助函数,它通过组合列表的元素来生成字符串。 其中一个字符串前面有一个“”。这样做的目的是允许用户输入一个数字(在 cList 中找到)或确切的文本(在 aList 的子列表中找到)。

老实说,我真的不确定自己哪里出错了,我尝试了一些不同的方法。

【问题讨论】:

  • 尝试计算第二行中的括号。

标签: python if-statement syntax syntax-error


【解决方案1】:

您在前一行缺少右括号:

response=(input((stringify(aList[i])))
#        1     23         4        432 ?

无论如何,您都在使用 许多 括号,以下就足够了:

response = input(stringify(aList[i]))
if response == cList[i] or " " + response in aList[i]:

如果您尝试同时访问来自aListbListcList 的元素,请考虑使用zip()

for a, b, c in zip(aList, bList, cList):
    response = input(stringify(a))
    if response == c or " " + response in a:

【讨论】:

  • ...如果 OP 刚刚插入 print(response) 它可能已经揭示了这一点
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-13
  • 2018-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多