我试图理解result=" "这段代码中的语句。此代码用于创建首字母缩略词。
def initials(phrase):
words = phrase.split()
**result = ""**
for word in words:
result += word[0]
return result.upper()
Run Code Online (Sandbox Code Playgroud)
result += word[0]
Run Code Online (Sandbox Code Playgroud)
这(大致)相当于
result = result + word[0]
Run Code Online (Sandbox Code Playgroud)
现在,如果我们省略该result = ""行,那么这是
result = <thing that doesn't exist> + word[0]
Run Code Online (Sandbox Code Playgroud)
这是一个错误。我们需要初始化 result为一个起始值,一个空短语有一个空首字母缩略词,所以""是一个很好的候选
| 归档时间: |
|
| 查看次数: |
46 次 |
| 最近记录: |