2016-04-11 25 views
0

Python新手,試圖弄清楚如何使用它。NameError:在for循環中未定義名稱'x'

使用下面的代碼給我一個錯誤:

NameError: name 'x' is not defined?

這是否與在for循環使用and?我該如何解決它?

lis = list(range(101)) 
lis1 = lis[::-1] 
tot = 0 
for i in range(101) and x in lis[:-1]: 
    tot = i + x 
    print(tot) 
+1

我已經重構你的問題了一下,試圖給它一個更有意義的標題,並添加了「巨蟒」的標籤。 –

+2

目前還不清楚他是否想總結兩個列表中相同位置上的元素,或者他是否想要使用第二個列表中的任何元素來總結第一個列表中的任何元素。 – Daniele

回答

0

你可以只使用

lis = list(range(101)) 
lis1 = lis[::-1] 
tot = 0 
for i in range(101): 
    for x in lis[:-1]: 
     tot = i + x 
     print(tot)