擅长:python、mysql、java
<p>为什么您的输入一直以<code>str</code>而不是<code>int</code>的形式读取,是因为<code>input()</code>返回一个字符串对象(自从他们从python2中删除了<code>raw_input()</code>函数并用<code>input()</code>函数代替它之后,情况就一直如此)。你知道吗</p>
<p>使用<code>int()</code>函数将字符串更改为整数,如下所示:</p>
<pre><code>amount = int(input("Enter amount: "))
</code></pre>
<p>(这也适用于<code>float()</code>函数。)</p>
<p>但是,如果用户输入一个字符串,这将产生一个错误。要避免这种情况,请将转换包装到<code>try</code>…<code>except</code>块中的整数:</p>
<pre><code>try:
amount = int(input("Enter amount: "))
except ValueError:
#Perhaps prompt the user to try again here
</code></pre>
<p>(同样,这将与<code>float()</code>函数一起工作)</p>