我正在通過2014年的hp代碼戰爭的東西,我打這個,不能失去知覺。我不斷收到一個運行時錯誤,提示「列表索引超出範圍」。爲什麼在這個for循環中列表索引超出範圍?
upc = input("please enter the first upc")
upc1 = upc.split()
ite = 0
cd = 0
for x in range (0,6):
cd = cd + int(upc1[ite])
ite = ite + 2
print (cd)
cd = cd * 3
print (cd)
ite = 1
for x in range (1,12, 2):
cd = cd + int(upc1[x])
print (cd)
cd = cd % 10
cd = 10 - cd
print (upc + str(cd))
的問題是第13行,這是這一行:
for x in range (1,12, 2):
cd = cd + int(upc1[x])
'1,3,5,7,9,11'...... upc1必須小於其中的一個值 – 2015-02-23 17:39:29
這是真正的縮進嗎? – Makoto 2015-02-23 17:40:23
我輸入0 1 2 3 4 5 6 7 8 9 0.它在for循環的第一次迭代時命中 – 2015-02-23 17:42:05