【发布时间】:2013-04-14 22:18:38
【问题描述】:
我有以下 python 代码:
x = range(0,10)
print x
for number in x:
print(number)
if number%2<> 0:
x.remove(number)
print x
奇怪的是,输出是这样的:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
0
1
3
5
7
9
[0, 2, 4, 6, 8]
第一行和最后一行是对的,但是为什么2、4、6、8没有打印出来呢? print 语句不在 if 语句中!
我在 Windows 7 上使用 python(x,y)。另外,我是 Python 新手...我习惯于 C++
【问题讨论】:
-
我第一次看到有人使用帕斯卡
<>。说真的,从现在开始使用!=,<>在 Python 3 中消失了,除非你from __future__ import barry_as_FLUFL -
不要使用
<>;它很久以前就被弃用了,并且从 Python 3 中消失了。请改用!=。 -
谢谢,我知道!=,我正在尝试不同的东西,因为它没有按照我想要的方式工作。 (我是 python 新手)
标签: python windows if-statement for-loop