0

I've looked up the continue key word but I'm still not quite understanding what role its playing in the if statements. I'm assuming this says continue on and treats the elif like an if statement, but I'm not sure why not just use an if statement instead of an elif statement with continue.

If that is the case, all those those conditional statements need to be checked no matter if is true or false. Why not just use an if statement instead of an elif?

If I'm understanding continue correctly, what is the reason for the last one because a new if statement is right after it? Wouldn't it naturally just continue to that if statement?

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))
Sean Francis N. Ballais
  • 2,338
  • 2
  • 24
  • 42
Brandon
  • 1,099
  • 1
  • 9
  • 14

2 Answers2

3

"continue" applies to loops.

It will simply stop the current iteration and jump into the next. So basically keep typing what you are typing until you type what the program expects of you.

Andrei Dragotoniu
  • 6,155
  • 3
  • 18
  • 32
1

Understand that continue does not break out of if and elif statements. Continue only breaks out of looping statements.

If continue is used within an if block which is present inside a loop, it skips the current iteration of the loop. So when continue is encountered, control moves to the beginning of the loop skipping any statements after continue. In this program, if user enters 'multiple letters', an 'already guessed letter' or a 'non-alphabet', continue comes into play and the program just skips that iteration and goes on to accept another letter.

while cond1:
    if cond2:
        continue
    #code

So here, if cond2 is satisfied, continue is encountered and #code is not executed. It just goes to the while cond1: again and continues the loop