擅长:python、mysql、java
<p>这里有一个简单的方法,用<code>while</code>循环而不是<code>for</code>循环来编写程序。代码在适当的时候会跳出无限循环。在</p>
<pre><code>def main():
secret_word = 'python'
correct_word = 'yo'
iterator = iter(secret_word)
sentinel = object()
while True:
item = next(iterator, sentinel)
if item is sentinel:
break
print(item if item in correct_word else '_', end=' ')
if __name__ == '__main__':
main()
</code></pre>
<p>它使用类似于<code>for</code>循环在内部实现的逻辑。或者,该示例可以使用异常处理。在</p>