Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Letter Game App Letter Game Introduction

brandonlind2
brandonlind2
7,823 Points

What is the reason for continue?

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 in stead of a elif statement with continue if that is the case and 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!)
    #what is this>>>  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))

2 Answers

Peter Lawless
Peter Lawless
24,404 Points

So all of this code is nested in a While loop, right? The continue command serves to start a new iteration of the loop immediately without getting down to the "if guess in word" portion of the loop. For all the cases here, it serves to make sure that the user can not provide invalid input, and immediately goes back to the start of the loop if he or she does! Imagine if I went into Papa John's and tried ordering a coffee, the cashier saying "Hey, we don't sell coffee here, what kind of pizza do you want?" is effectively restarting the pizza-ordering "loop" with a "continue" command.

If use continue, strikes won't count, otherwise, you give input 1, it will show you "you can only guess letters" and strikes will + 1