在这些行中:
foo = []
a = foo.append(raw_input('Type anything.\n'))
b = raw_input('Another questions? Y/N\n')
while b != 'N':
b = foo.append(raw_input('Type and to continue, N for stop\n'))
if b == 'N': break
print foo
如何做循环中断?谢谢!
在这些行中:
foo = []
a = foo.append(raw_input('Type anything.\n'))
b = raw_input('Another questions? Y/N\n')
while b != 'N':
b = foo.append(raw_input('Type and to continue, N for stop\n'))
if b == 'N': break
print foo
如何做循环中断?谢谢!
list.append 返回无。
a = raw_input('Type anything.\n')
foo = [a]
b = raw_input('Another questions? Y/N\n')
while b != 'N':
b = raw_input('Type and to continue, N for stop\n')
if b == 'N': break
foo.append(b)
这是这样做的方法
foo = []
a = raw_input('Type anything.\n')
foo.append(a)
b = raw_input('Another questions? Y/N\n')
while b != 'N':
b = raw_input('Type and to continue, N for stop\n')
if b == 'N': break
foo.append(raw_input)
print foo
只需检查添加到的最后一个元素foo
:
while b != 'N':
foo.append(raw_input('Type and to continue, N for stop\n'))
if foo[-1] == 'N': break # <---- Note foo[-1] here
您将 b 分配给列表附加的结果,该结果为无。即使您正在查看 foo,您也会查看由 foo.append 创建的列表,然后将其与字符“N”进行比较。即使您只在输入中键入 N,foo 的值也至少看起来像 ['N']。您可以通过以下方式完全消除 b:
while True:
foo.append(raw_input('Type and to continue, N for stop\n'))
if 'N' in foo: break
尽管这会将“N”字符留在您的列表中。不确定这是不是有意的。