Python 有人能解释一下if语句中的continue键吗?

Python 有人能解释一下if语句中的continue键吗?,python,Python,我已经查找了continue关键字,但我仍然不太了解它在if语句中扮演的角色。我假设这表示continue on,并将elif视为if语句,但我不确定为什么不使用if语句而不是elif语句来处理continue 如果是这种情况,则无论是否为true或false,都需要检查所有这些条件语句。为什么不使用if语句而不是elif 如果我正确理解了continue,那么上一条语句的原因是什么,因为后面紧跟着一条新的If语句?它会自然地继续那个if语句吗 while True: start= in

我已经查找了
continue
关键字,但我仍然不太了解它在
if
语句中扮演的角色。我假设这表示continue on,并将
elif
视为
if
语句,但我不确定为什么不使用
if
语句而不是
elif
语句来处理
continue

如果是这种情况,则无论
是否为
true
false
,都需要检查所有这些条件语句。为什么不使用
if
语句而不是
elif

如果我正确理解了
continue
,那么上一条语句的原因是什么,因为后面紧跟着一条新的
If
语句?它会自然地继续那个
if
语句吗

while True:
    start= input('Press q to quite, enter any other key to start')
    if start.lower()=='q':
        break
    #pick a random words
    word=random.choice(words)
    wrong_guesses=[]
    right_guesses=[]
    while len(wrong_guess) < 7 and len(right_guesses) != len(word)
        #draw spaces
        for letter in word:
            if letter in right_guesses:
                print(letter, end='')
            else:
                print('_', end='')
            print('')
            print('strikes: {}/7'.format(len(bad_guesses))
            print('')

   #take guess
    guess= input('guess a letter: ').lower()
    if len(guess) != 1:
        print('You can only guess a sinlge letter!')
    #what is this>>>    continue
    elif guess in wrong_guesses or guess in right_guesses:
        print('you\'ve already guessed that letter!')
        continue
    elif not guess.isalpha():
        print('you can only guess letters!')
    #what is this>>>    continue
    if guess in word:
        right_guesses.append(guess)
        if len(right_guesses)==len(list(word)):
            print('You win! The word was {}'.format(list(word))
            break
        else:
            wrong_guesses.append(guess)
    else:
        print('you didnt guess it! My secret word was {}'.format(word))
为True时:
开始=输入('按q键停止,输入任何其他键开始')
如果start.lower()
打破
#随便挑一个单词
单词=随机。选择(单词)
猜错了
右猜=[]
而len(猜错)<7和len(猜对)!=蓝(字)
#画空间
对于大写字母:
如果右边的字母猜到:
打印(字母,结尾=“”)
其他:
打印(“”,结束=“”)
打印(“”)
打印('strikes:{}/7'。格式(len(错误猜测))
打印(“”)
#猜猜看
猜=输入('猜一个字母:')。下()
如果len(猜测)!=1:
打印('你只能猜一个字母!')
#这是什么>>>继续吗
elif猜错或猜对:
打印(“你已经猜到了那封信!”)
持续
elif不猜。isalpha():
打印('您只能猜测字母!')
#这是什么>>>继续吗
如果用词猜测:
右猜测。追加(猜测)
如果len(右猜)=len(列表(单词)):
打印('你赢了!单词是{}'。格式(列表(单词))
打破
其他:
猜错了。追加(猜)
其他:
打印('你没猜到!我的秘密单词是{}'。格式(单词))
“继续”适用于循环

它只需停止当前迭代并跳转到下一个迭代。因此,基本上一直键入您正在键入的内容,直到您键入程序对您的期望。

继续”适用于循环


它只会停止当前迭代并跳转到下一个迭代。因此,基本上一直键入您正在键入的内容,直到您键入程序对您的期望。

请理解continue不会中断if和elif语句。continue只中断循环语句

如果在循环中存在的If块中使用continue,它将跳过循环的当前迭代。因此,当遇到continue时,控制将移动到循环的开头,跳过continue后的任何语句。在该程序中,如果用户输入“多个字母”、“已猜到的字母”或“非字母”,则会出现continue进入游戏,程序只是跳过该迭代,继续接受另一封信

while cond1:
    if cond2:
        continue
    #code

因此,在这里,如果满足cond2,将遇到continue,并且不执行#代码。它只会转到while cond1:再次并继续循环

请理解continue不会中断if和elif语句。continue只中断循环语句

如果在循环中存在的If块中使用continue,它将跳过循环的当前迭代。因此,当遇到continue时,控制将移动到循环的开头,跳过continue后的任何语句。在该程序中,如果用户输入“多个字母”、“已猜到的字母”或“非字母”,则会出现continue进入游戏,程序只是跳过该迭代,继续接受另一封信

while cond1:
    if cond2:
        continue
    #code

因此,在这里,如果满足cond2,将遇到continue,并且不执行#code。它只是转到while cond1:再次并继续循环

continue语句引用外部循环,而不在if语句中。
continue
将直接将您发送到
while
循环的下一次迭代(不运行否则将运行的任何循环)continue语句引用外部循环,而不在if语句中。
continue
将直接将您发送到
循环的下一次迭代(不运行否则将运行的任何循环)