-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
我想也許括號是在錯誤的空間,但如果我把他們其他地方,我得到一個語法錯誤。
'wheel1'是一個列表,您可以使用括號(運算符)訪問列表元素。所以它應該是'wheel1 [spin1]'而不是'wheel1(spin1)'。而對'str'的調用應該使用圓括號,所以你的print語句應該是'print(str(wheel1 [spin1]))' – UnholySheep