【发布时间】:2019-11-30 19:17:21
【问题描述】:
""" 如果每个字符出现多次,则返回 -1。示例输入:s: "Who wants hot watermelon?。输出:8。"""
def findLastIndex(str, x):
index = -1
for i in range(0, len(str)):
if str[i] == x:
index = i
return index
# String in which char is to be found
str = "Who wants hot watermelon"
# char whose index is to be found
x = 's'
index = findLastIndex(str, x)
if index == -1:
print("Character not found")
else:
print(index)
【问题讨论】:
-
顺便说一句,输入字符串的输出应该是 0,因为大写的
W只出现一次。