我们是初学者,想用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
答案 0 :(得分:0)
中断应该起作用。如果您想跳过这种猜测,可以使用
$('.navbar-nav li').each(function () {
let anchor = $(this).children('a');
if ($(anchor).attr('href') == url) {
$(this).addClass('active');
}
});