擅长:python、mysql、java
<ul>
<li>使用<code>break</code>中断while循环:</li>
<li>不需要测试<code>answer</code>是否在<code>('yes',)</code>中,因为
<code>while True</code>默认情况下循环将继续循环:</li>
<li><code>answer in ('no')</code>与<code>answer in 'no'</code>相同,后者只会
如果<code>answer</code>是<code>'n'</code>或<code>'o'</code>或<code>'no'</code>,则为True。那可能不是
你的意思。最好使用<code>answer == 'no'</code>。如果您在<code>answer in ('no',)</code>中添加了逗号,那么<code>('no',)</code>将是一个元组,并且如果<code>answer</code>等于元组中的一个项,则满足条件。逗号在这里有很多意义!</li>
</ul>
<hr/>
<pre><code>def add_country():
while True:
new_short=raw_input('Country Name in short:')
new_full=raw_input('Country Name in full:')
countries[new_short]=new_full
answer=raw_input('want to add more?')
if answer == 'no':
break
print countries
</code></pre>