python-3.x - Python:试图理解这个函数是如何工作的以及它的目的是什么
问题描述
我有一个任务来解释以下功能的工作原理。我是编程世界的新手,我真的很难理解这个函数的用例可能是什么。我也有点迷失试图理解函数的步骤。
我已经评论了此功能中发生的大多数操作,如果我有任何错误或误解,请随时纠正我。
def find(word, letter):
# Function with 'word' an 'letter' as parameter
index = 0
# Set index to 0
while index < len(word):
# While index has a smaller letter count than then the given 'word', proceed
if word[index] == letter:
# If one of the indexed letters from 'word' is equal to 'letter', proceed
return index
# Return to index underneath here
index += 1
# Add the value of '1' to 'index' for every time the program loops through 'word'
return -1
# Here i have no clue
我真的很感谢所有帮助,很抱歉提出所有这些菜鸟问题,但我无处可去。
解决方案
这个函数很简单,它的作用就是在word参数中查找字母参数,如果找到返回字母参数在word中的位置,如果没有找到返回-1表示没有找到。如果你是新手,可以从入门教程开始,虽然你可以在这里找到答案,但这不是正确的方法。
推荐阅读
- android - 循环查找文件而不让操作系统杀死我的应用程序
- c# - 会话超时在 web.config 文件中不起作用
- mysql - 使用MYSQL根据给定日期获取当前期间
- python - groupby.mean() 不起作用,而 sum()、std() 和 size() 都起作用
- r - 如何转换数据并将表头添加到表中
- python - 如何提取带有三个正斜杠的字符串?
- r - 如何更改 Sweave/Knitr 的输出文件名/目录?
- python-3.x - 如何在 Pandas DataFrame 中组合/覆盖 2 列
- asp.net - 我在使用 asp.net core 2.2 应用程序的生产中的 facebook 登录回调中收到错误 500
- sql - SQL 查询为表中的每对唯一值选择第一行