【发布时间】:2016-10-16 10:52:18
【问题描述】:
我查找了continue 关键字,但我仍然不太了解它在if 语句中的作用。我假设这表示 continue 并将elif 视为if 语句,但我不确定为什么不使用if 语句而不是elif 语句continue。
如果是这种情况,无论if 是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))
【问题讨论】:
-
continue 语句是指外循环,而不是在 if 语句中。
-
continue将直接将您发送到while循环的下一次迭代迭代(不运行其他情况下会运行的任何内容)
标签: python