范围函数语法enter image description here
中的错误for x in range(20),
if x % 2 == 0
print x
else
print 'odd',
输出:
File "<ipython-input-106-a3bbe30e4016>", line 1
for x in range(20),
^
SyntaxError: invalid syntax
答案 0 :(得分:1)
只需用:
替换第一行中的逗号
然后,在Python 3.x中,编写print("toto")
而不是print "toto"
。
最后,if
条件的结尾需要一个:
(例如在foor
或while
循环中)
for x in range(20):
if x % 2 == 0:
print(x)
else:
print('odd')