2016-04-21 112 views
1
abc_str = raw_input('A B C: ') 
print abc_str 
abc_list = abc_str.split() 
print abc_list 
# suuuum = 0 
for i in range(3): 
    suuuum += int(abc_list[i]) 
print suuuum 

Traceback (most recent call last): 
    File "tttest.py", line 7, in <module> 
    suuuum += int(abc_list[i]) 
NameError: name 'suuuum' is not defined 

如果我省略尖銳,一切都會好的。但爲什麼我應該先定義「suuuum」? 我的答案是因爲我在將它分配給一個對象之前稱爲「suuuum」。然後我在終端試了一個+ = 8,如下:關於未定義的名稱錯誤

>>> a += 8 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
NameError: name 'a' is not defined 

這是我的想法。我對嗎?

回答

1

你說得對。當你寫x += 1,它的意思如下:

x = x + 1 

所以,如果你沒有定義x已經,你得到一個錯誤。因爲解釋者無法計算出平等的右邊。

for i in range(3): 
    suuuum = suuuum + int(abc_list[i]) 

從而爲第i個,在右側的suuuum是不確定的:

在你的程序上面,當你評論suuuum,你是因爲你有以下得到了錯誤。

+0

謝謝! (我嘗試了upvote,但它說我需要15個聲望:\) –