2016-10-23 29 views
-3

我有一個任務來創建一臺老虎機。我正在嘗試編寫將使機器旋轉的循環,但它一直說我的列表不可調用。列表對象不可調用

這裏就是我遇到的問題:

wheel1 = ['Candy','Ghost','Pumpkin','Cat','Zombie','Witch','Witch','Cat','Ghost','Candy'] 
wheel2 = ['Candy','Ghost','Pumpkin','Cat','Zombie','Witch','Cat','Pumpkin','Ghost','Candy'] 
wheel3 = ['Candy','Ghost','Pumpkin','Cat','Zombie','Witch','Pumpkin','Candy','Candy','Ghost'] 

#loop to spin 
i = 0 

while i < 500: 
    spin1 = random.randint(0,9) 
    spin2 = random.randint(0,9) 
    spin3 = random.randint(0,9) 
    print(str[wheel1(spin1)]) 
    i += 1 

我想也許括號是在錯誤的空間,但如果我把他們其他地方,我得到一個語法錯誤。

+1

'wheel1'是一個列表,您可以使用括號(運算符)訪問列表元素。所以它應該是'wheel1 [spin1]'而不是'wheel1(spin1)'。而對'str'的​​調用應該使用圓括號,所以你的print語句應該是'print(str(wheel1 [spin1]))' – UnholySheep

回答

0

更新行print(str[wheel1(spin1)])print(str(wheel1[spin1]))

這個問題是當你做wheel1(),python把它當作函數,並嘗試調用該函數。但是由於您有list並且想要訪問該值,因此語法爲wheel1[i],其中i是您要訪問的索引。

此外,您正在做str[..],但相反它應該是str(..),由於上述相同的原因。

+1

非常感謝!修正它完美。 – ALLCAPS