0

I am a beginner in Python and from what I understand, the continue statement in Python returns the control to the beginning of the while loop.

guesses = [0]
    
while True:
    # we can copy the code from above to take an input
    guess = int(input("I'm thinking of a number between 1 and 100.\n  What is your guess? "))
    
    if guess < 1 or guess > 100:
        print('OUT OF BOUNDS! Please try again: ')
        continue
    
    # here we compare the player's guess to our number
    if guess == num:
        print(f'CONGRATULATIONS, YOU GUESSED IT IN ONLY {len(guesses)} GUESSES!!')
        break
        
    # if guess is incorrect, add guess to the list
    guesses.append(guess)
    
    # when testing the first guess, guesses[-2]==0, which evaluates to False
    # and brings us down to the second section
    if guesses[-2]:  
        if abs(num-guess) < abs(num-guesses[-2]):
            print('WARMER!')
        else:
            print('COLDER!')
   
    else:
        if abs(num-guess) <= 10:
            print('WARM!')
        else:
            print('COLD!')

Above is the code for the game called 'guess the number from 1 - 100'.

The first if statement where guess < 1 or guess > 100, it will print "Out of bounds!" and then continue which loops to the top of the code and asks for the user's input again.

But for the 3rd if statement where if guesses[-2]:, it does not require continue for neither if nor else.

Sorry if you do not understand what I am asking. But essentially, I want to know why 'continue' statement is not required after print('WARMER!), print('COLDER!'), print('WARM!') and print('COLD!').

guesses = [0]

while True:
    # we can copy the code from above to take an input
    guess = int(input("I'm thinking of a number between 1 and 100.\n  What is your guess? "))
    
    if guess < 1 or guess > 100:
        print('OUT OF BOUNDS! Please try again: ')
        continue
    
    # here we compare the player's guess to our number
    if guess == num:
        print(f'CONGRATULATIONS, YOU GUESSED IT IN ONLY {len(guesses)} GUESSES!!')
        break
        
    # if guess is incorrect, add guess to the list
    guesses.append(guess)
    
    # when testing the first guess, guesses[-2]==0, which evaluates to False
    # and brings us down to the second section
    if guesses[-2]:  
        if abs(num-guess) < abs(num-guesses[-2]):
            print('WARMER!')
            **continue**
        else:
            print('COLDER!')
            **continue**
   
    else:
        if abs(num-guess) <= 10:
            print('WARM!')
            **continue**
        else:
            print('COLD!')
            **continue**
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Oscar
  • 9
  • 3
  • 1
    In the case of out of bounds, `continue` makes the code loop without going through the code underneath, so it avoids all the tests. If there is no code to skip underneath, there's no need for `continue`. – Thierry Lathuille Nov 18 '20 at 11:36
  • Oh so in this case, even though it has no code underneath, it will automatically go back up to the beginning of the 'while true' loop without requiring 'continue'? – Oscar Nov 18 '20 at 11:46

4 Answers4

0

If you want to know why the continue is not always in if or else, this is due to the fact that when the operation arrives (true), the loop is exited and the code continues to run.

liakoyras
  • 1,101
  • 12
  • 27
  • Yes but lets say if I were to add 'continue' after print('WARMER!), print('COLDER!'), print('WARM!') and print('COLD!'), does it change anything? – Oscar Nov 18 '20 at 11:40
0

Note that all four of these print statements are the last of their execution branch.

Meaning, if guesses[-2] evaluates as true, than the else part won't be executed at all. Then, if abs(num-guess) < abs(num-guesses[-2]) evaluates to true, again - its else won't be executed. It means that for this execution branch the print('WARMER!') is the last statement of the loop and hence continue is not needed.

Same logic applies to all other 3 print statements.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
0

In the case "guess < 1 or guess > 100" you want to skip the rest of the loop since you dont want to append the guess to the guesses list. Therefore you use continue.

The 'continue' statement is not required after print('WARMER!) etc because you dont need to skip any code afterwards. In this case because there wont be any lines executed after the print statement anyway.

BanjoBenjo
  • 98
  • 8
0

First of all, if/else doesn't require continue. It's optional.
From what I understand you want to know why you used continue in the first if/else and not in the last if/else.

In the first one, after executing if statement it will transfer the control to while True: but for the last if/else statement it is not needed as the control will automatically go to while True: as it is the end of the loop and even if you had used continue it wouldn't have made a difference.

For further references