擅长:python、mysql、java
<p>需要使用<code>if</code>,因为您只想计算特定字符<code>char</code>的实例。如果没有它,您将最终对字符串中的每个字符执行<code>count = count + 1</code>,这样就可以得到完整的字符串长度,而不是要查找的特定字符的数量。在</p>
<p>代码中有注释:</p>
<pre><code>for c in word: # go through each character in code
if char == c: # if the character is the one we're counting
count = count + 1 # add one to the current count of characters
</code></pre>