擅长:python、mysql、java
<p>您必须使用<code>raw_input()</code>(Python 2.x),因为<a href="http://docs.python.org/2/library/functions.html#input" rel="noreferrer">^{<cd2>}</a>等同于<code>eval(raw_input())</code>,所以它会将您的输入作为有效的Python表达式进行分析和计算。</p>
<pre><code>while True:
s = raw_input('Enter something : ')
if s == 'quit':
break
print('Length of the string is', len(s))
print('Done')
</code></pre>
<p><strong>注意:</strong></p>
<p><code>input()</code>不会捕获用户错误(例如,如果用户输入了一些无效的Python表达式)。<code>raw_input()</code>可以这样做,因为它将输入转换为<code>string</code>。<a href="http://docs.python.org/2/library/functions.html#raw_input" rel="noreferrer">For futher information, read Python docs</a>。</p>