2013-08-01 48 views
0

在這些線路:而與創造的raw_input無限循環

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 

如何做好迴路斷線? 謝謝!

+0

什麼不起作用? – Stephan

+0

你假設錯誤的問題。要調試這個,在循環 –

回答

1

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) 
+1

中打印'foo'是一個好主意,如果他想使用'a',它不會是'raw_input'返回,但無論'foo.append'返回什麼結果 – Blam

+0

@Blam People所有這個問題似乎忘記了'list.append()'不會返回任何東西。 –

+0

謝謝,gnibbler! – bobthepanda

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 
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 
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」字在列表中。不知道這是否是有意的。