擅长:python、mysql、java
<p>试试这个:</p>
<pre><code>while True:
# main program
while True:
answer = raw_input('Run again? (y/n): ')
if answer in ('y', 'n'):
break
print 'Invalid input.'
if answer == 'y':
continue
else:
print 'Goodbye'
break
</code></pre>
<p>内部while循环,直到输入为<code>'y'</code>或<code>'n'</code>。如果输入是<code>'y'</code>,while循环将再次启动(<code>continue</code>关键字跳过剩余的代码,直接进入下一个迭代)。如果输入是<code>'n'</code>,则程序结束。</p>
<p>还要注意,将<code>raw_input</code>转换为<code>str()</code>是多余的,因为<code>raw_input</code>已经返回一个字符串。</p>