而raw_input创建一个无限循环

时间:2013-08-01 00:13:38

标签: python

在这些方面:

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

如何进行循环中断?  谢谢!

4 个答案:

答案 0 :(得分:1)

list.append返回None。

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)

答案 1 :(得分:0)

这是做到这一点的方法

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

答案 2 :(得分:0)

只需检查添加到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

答案 3 :(得分:0)

您要将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”字符。不确定是否有意。

相关问题