繁体   English   中英

Python:在while循环中结束执行

[英]Python: end execution in while loop

我们是初学者,想用Python编写游戏Mastermind。 (请参阅下面的代码。)问题:如果“ while”循环中发生“ StopIteration”错误,我们想结束代码的执行。 但是以某种方式'quit()'在这个地方不起作用。 谁能给我们一个解决这个问题的线索?

def inconsistent(new_guess, guesses):
   for guess in guesses:
      res = check(guess[0], new_guess)
      (rightly_positioned, right_colour) = guess[1]
      if res != [rightly_positioned, right_colour]:
         return True # inconsistent
   return False # i.e. consistent

while inconsistent (new_guess, guesses):
        try:
            new_guess=next(generator)
        except StopIteration:
            print("Error: Your answers were inconsistent!")
            break

休息应该工作。 如果您想跳过此猜测,可以使用

continue

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM