Python 找出第一个元素出现的index,如果从没出现,则打印-1

从用户那里获得一个单词,并打印出字母'e'第一次出现的索引.写一个loop来寻找这个索引。如果'e'从未出现过,则打印-1。

word = input("请输入单词")
for index, s in enumerate(word):
    if s == "e":
        print(index)
        break
else:
    print(-1)

img

img

如果觉得答案对你有帮助,请点击下采纳,谢谢~

s=input("please put a stirng here:")
for i in s:
if i=="e":
当写到这一步时候,想要用index()来得到第一个e出现的index,但是由于必须使用loop,就写不出来.

def loop(n):
a = 0
b = -1
for i in n:
if i == "e":
b = a
a += 1
return b