str.发现返回语句不正确,虽然我不知道为什么


def find_first_occurrence(msg: str, string_to_find: str) -> int:
    return str.find(msg,string_to,find)

这是问题所在,尽管我的返回语句是错误的,但我应该完成/创建返回语句。我真的不知道我在做什么错,但是当我测试它时,显然是错误的。似乎是一个简单的问题,但我已经陷入了几个小时!

这可能是您要做的:

def find_first_occurrence(msg, string_to_find):
    # looks like it wants you to not care about case, as 'H' returns 8 on an 'h'
    string_to_find = string_to_find.lower();
    msg = msg.lower();
    return msg.find(string_to_find);
print find_first_occurrence("October holidays: Halloween and Thanksgiving", "H");

编辑每个回复:

str.find(val)返回val的位置。它是对病例敏感的,因此在" Hello,Hello"中,str.find("h")将返回7,str.find("H")将返回0。

问题表明,它不应通过为您提供>>> find_first_occurrence('October holidays: Halloween and Thanksgiving', 'H') 8行来考虑套管。

通常,'H'将获得位置18,因为H是国会大厦。但是他们说您应该得到较低案例的位置8,这表明套管不重要。

通过执行string_to_find = string_to_find.lower();msg = msg.lower();,您可以将案件全部删除,因此它将在所有低案例字符串上找到第一次出现下案字母。

相关内容

最新更新