它总是给我它第一次出现的时候.index()
。我希望索引等于列表的位置。例如,This word appears in the places, 0,4
。如果用户输入了 chicken,那将是输出。
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")