擅长:python、mysql、java
<p>正如回溯所说,错误就在这里:</p>
<pre><code>totalPrice=sum(int(priceItems[0:]))
</code></pre>
<p>这一行实际上包含一些错误:</p>
<ol>
<li><p><code>priceItems[0:]</code>是从<code>priceItems</code>开始到结束的片段-换句话说,<code>[0:]</code>什么也不做。这并不是一个错误,但它是毫无意义的,并表明你并不真正知道你要用它来实现什么。</p></li>
<li><p><code>int(priceItems[0:])</code>正在尝试将列表转换为整数,这显然行不通。</p></li>
<li><p>如果您能够以某种方式将列表转换为整数,<code>sum(int(priceItems[0:]))</code>将尝试获取该整数的和,这也没有意义;您对一组数字求和,而不是一个数字。</p></li>
</ol>
<p>相反,请使用函数<a href="https://docs.python.org/3/library/functions.html#map" rel="nofollow">^{<cd6>}</a>:</p>
<pre><code>totalPrice = sum(map(int, priceItems))
</code></pre>
<p>这将获取列表中的每个项,将其转换为整数并对结果求和。</p>
<p>不过,请注意,整件事可以写成:</p>
<pre><code>print('Your total is:', sum(map(int, iter(lambda: input('Please enter the prices of your items, once you are done type: total.'), 'total'))))
</code></pre>