它總是第一次出現.index()
。我希望索引等於列表的位置。例如,This word appears in the places, 0,4
。如果用戶輸入了可能是輸出的雞。如何多次顯示列表中的項目?
dlist = ["chicken","potato","python","hammer","chicken","potato","hammer","hammer","potato"]
x=None
while x != "":
print ("\nPush enter to exit")
x = input("\nGive me a word from this list: Chicken, Potato, Python, or Hammer")
y = x.lower()
if y in dlist:
count = dlist.count(y)
index = dlist.index(y)
print ("\nThis word appears",count,"times.")
print ("\nThis word appears in the places",index)
elif y=="":
print ("\nGood Bye")
else:
print ("\nInvalid Word or Number")
究竟是什麼問題?你可以發佈你的預期輸出和你當前的輸出嗎? – dckrooney