Python - If 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1305302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python - If statement
提问by Adam Chetnik
I keep getting invalid syntax with the below:
我不断收到以下无效语法:
if (row[0] == year) and (row[1] == month) and (row[2] == day and (row[3] == hour) and (row[4] == minute):
print "hello"
else:
print "hello2"
Any ideas?
有任何想法吗?
回答by truppo
If "row" is a list, you can do this instead (for clarity):
如果“行”是一个列表,您可以改为执行此操作(为清楚起见):
if row[:5] == [year, month, day, hour, minute]:
..or if row is a tuple:
..或者如果 row 是一个元组:
if row[:5] == (year, month, day, hour, minute):
回答by angus
A closing parenthesis is missing at row[2] == day
.
缺少右括号row[2] == day
。
回答by Konrad Rudolph
You're missing a closing parenthesis:
你缺少一个右括号:
if (row[0] == year) and (row[1] == month) and (row[2] == day and (row[3] == hour) and (row[4] == minute):
^^^
Also, beware indentation.
另外,要注意缩进。
回答by John Machin
You have 5 superfluous left parentheses but only 4 superfluous right parentheses. Consider losing ALL the parentheses!
您有 5 个多余的左括号,但只有 4 个多余的右括号。考虑丢失所有括号!
回答by Aaron Digulla
Try:
尝试:
if row[0:5] == [year, month, day, hour, minute]:
That fixes your error and makes the whole thing much more readable.
这修复了您的错误并使整个事情更具可读性。
回答by Aaron Digulla
Add a colon after "hour)" along with the aforementioned ")"
在“小时)”后面加上一个冒号以及前面提到的“)”
回答by gancalo scott
it might be because you do not need indentations on the else.
这可能是因为你不需要在 else 上缩进。
if ...
print ...
else:
...