<p><strong>TLDR</strong></p>
<ul>
<li>Python 3不计算用<code>input</code>函数接收的数据,但Python 2的<code>input</code>函数计算(阅读下一节了解其含义)。</li>
<li>Python 2相当于Python 3的<code>input</code>函数。</li>
</ul>
<p><strong>Python 2.x</strong></p>
<p>有两个函数可以获取用户输入,分别称为<a href="https://docs.python.org/2/library/functions.html#input" rel="noreferrer">^{<cd1>}</a>和<a href="https://docs.python.org/2/library/functions.html#raw_input" rel="noreferrer">^{<cd4>}</a>。它们之间的区别是,<code>raw_input</code>不计算数据,而是以字符串形式返回。但是,<code>input</code>将计算您输入的内容,并返回计算结果。例如</p>
<pre><code>>>> import sys
>>> sys.version
'2.7.6 (default, Mar 22 2014, 22:59:56) \n[GCC 4.8.2]'
>>> data = input("Enter a number: ")
Enter a number: 5 + 17
>>> data, type(data)
(22, <type 'int'>)
</code></pre>
<p>计算数据<code>5 + 17</code>,结果为<code>22</code>。当它计算表达式<code>5 + 17</code>时,它检测到您正在添加两个数字,因此结果也将是相同的<code>int</code>类型。因此,类型转换是免费的,并且<code>22</code>作为<code>input</code>的结果返回并存储在<code>data</code>变量中。你可以把<code>input</code>看作是由<a href="https://docs.python.org/2/library/functions.html#eval" rel="noreferrer">^{<cd18>}</a>调用组成的<code>raw_input</code>。</p>
<pre><code>>>> data = eval(raw_input("Enter a number: "))
Enter a number: 5 + 17
>>> data, type(data)
(22, <type 'int'>)
</code></pre>
<p><strong>注意:</strong>在Python 2.x中使用<code>input</code>时应该小心。我在<a href="https://stackoverflow.com/a/21122817/1903116">this answer</a>中解释了为什么使用它时应该小心。</p>
<p>但是,<code>raw_input</code>不计算输入,而是以字符串的形式返回。</p>
<pre><code>>>> import sys
>>> sys.version
'2.7.6 (default, Mar 22 2014, 22:59:56) \n[GCC 4.8.2]'
>>> data = raw_input("Enter a number: ")
Enter a number: 5 + 17
>>> data, type(data)
('5 + 17', <type 'str'>)
</code></pre>
<p><strong>Python 3.x</strong></p>
<p>Python 3.x的<a href="https://docs.python.org/3/library/functions.html#input" rel="noreferrer">^{<cd1>}</a>和Python 2.x的<code>raw_input</code>是相似的,并且<code>raw_input</code>在python3.x中不可用</p>
<pre><code>>>> import sys
>>> sys.version
'3.4.0 (default, Apr 11 2014, 13:05:11) \n[GCC 4.8.2]'
>>> data = input("Enter a number: ")
Enter a number: 5 + 17
>>> data, type(data)
('5 + 17', <class 'str'>)
</code></pre>
<hr/>
<p><strong>解决方案</strong></p>
<p>要回答您的问题,因为Python 3.x不计算和转换数据类型,您必须显式地转换为<code>int</code>s,使用<a href="https://docs.python.org/3/library/functions.html#int" rel="noreferrer">^{<cd12>}</a>,如下所示</p>
<pre><code>x = int(input("Enter a number: "))
y = int(input("Enter a number: "))
</code></pre>
<p>可以接受任意基数的数字,并使用<code>int</code>函数将其直接转换为基数10,如下所示</p>
<pre><code>>>> data = int(input("Enter a number: "), 8)
Enter a number: 777
>>> data
511
>>> data = int(input("Enter a number: "), 16)
Enter a number: FFFF
>>> data
65535
>>> data = int(input("Enter a number: "), 2)
Enter a number: 10101010101
>>> data
1365
</code></pre>
<p>第二个参数告诉输入的数字的基数,然后在内部理解并转换它。如果输入的数据是错误的,它将抛出一个<code>ValueError</code>。</p>
<pre><code>>>> data = int(input("Enter a number: "), 2)
Enter a number: 1234
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: invalid literal for int() with base 2: '1234'
</code></pre>
<p>对于可以有小数部分的值,类型应该是<code>float</code>,而不是<code>int</code>:</p>
<pre><code>x = float(input("Enter a number:"))
</code></pre>
<hr/>
<p>除此之外,你的程序可以稍微改变一下,像这样</p>
<pre><code>while True:
...
...
if input("Play again? ") == "no":
break
</code></pre>
<p>通过使用<code>break</code>和<code>while True</code>,可以去掉<code>play</code>变量。</p>