问题描述
def rect_extend(x):
m, n = 1
while 1 < x:
m = m + 1
n = n + 1
return m, n
此简单函数返回:
iPython中的
错误.我不知道为什么这样做,while
函数不起作用-条件似乎是true
.
error in iPython. I don't know why it does this, while
function doesn't work - condition seems to be true
.
(而的条件是有意简化的;原始代码没有此条件)
(while's condition was simplified on purpose; original code doesn't have it)
推荐答案
我认为你想要
m = 1
n = 1
或
m = n = 1
而不是m, n = 1
.
此(序列解压缩)[http://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences]:
This (sequence unpacking)[http://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences]:
x, y = z
与您认为的有所不同.
这实际上意味着:
x = z[0] # The first item in z
y = z[1] # The second element of z
例如,您可以执行以下操作:
For instance, you could do this:
x, y, z = (1, 2, 4)
然后:
>>> x
1
>>> y
2
>>> z
4
在您的情况下,这是行不通的,因为1
是整数,没有元素,因此会出错.
In your case, you this doesn't work, because 1
is an integer, it doesn't have elements, hence the error.
与元组(和splat运算符-*
)结合使用的序列拆包的有用功能:
Useful features of sequence unpacking combined with tuples (and the splat operator - *
):
此:
a, b = b, a
交换a
和b
的值.
解压range
,对常量有用:
>>> RED, GREEN, BLUE = range(3)
>>> RED
0
>>> GREEN
1
>>> BLUE
2
splat运算符:
>>> first, *middle, last = 1, 2, 3, 4, 5, 6, 7, 8, 9
>>> first
1
>>> middle
[2, 3, 4, 5, 6, 7, 8]
>>> last
9
这篇关于“'int'对象不可迭代"在一段时间内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!