擅长:python、mysql、java
<p>我认为你不需要使用<code>sum</code>。</p>
<p>试试这个:</p>
<pre><code>for (Name, count) in sorted(counts.iteritems()):
print 'Species total: %s' % count
</code></pre>
<p>或者,可能更好:</p>
<pre><code>for (Name, count) in sorted(counts.iteritems()):
print 'Total for species %s: %s' % (Name, count)
</code></pre>
<p><code>sum</code>用于当您有一个数字列表并希望找到该数字列表的总和时。
您已经使用<code>counts</code>收集了每个动物的总数——您只需要显示它。</p>
<p><strong>编辑</strong></p>
<p>要总结统计的动物总数,可以执行以下操作:</p>
<pre><code>total = sum(counts.values())
print 'Total number of animals: %d' % total
</code></pre>
<p><strong>编辑2</strong></p>
<p>计算的动物种类数只是<code>counts</code>字典的长度:</p>
<pre><code>print 'Number of kinds of animals: %d' % len(counts)
</code></pre>