How can I fix 'continue not properly in loop' error in my Python while loop?


I’m trying to make it so that my program will write the input again if the answer isn’t within 5 to 10 but when I try to use while True and continue it says that the continue is not properly in loop. It only fixes when I take away the space infront of the line but that creates an "syntax error" on the "else:" line.
I cannot move the "continue" forward one space or more or backwards any spaces either. same for the "while True" line.

`import random`
`number = random.randint(5,10)`
`name1 = input("What is your name? ")`
`while True:`
  `guess2 = int(input("Pick a number between 5 and 10n"))`

`if guess2 <5 or guess2 >10:`
  `print("this isn't a number between 5 and 10 please try again.") `
 ` continue`

 `print("nYour guess was", guess2)` 
 `print("the computer guessed", number)`

`if guess2 != number:`
 ` print(name1 + " you have lost try again")`
` print(name1 + " you have Won the game try again if you want to")`

`print("nNAME:",name1,"nUSER GUESS:",guess2,"nCOMPUTER CHOICE:",number,"nGAME OVER")`

I have tried looking up an explanation but to no avail since they don’t work either.

Asked By: viktor



Continue is a statement for flow control, used to continue the iteration over the next value. In this case, it’s used in a for loop.

for number in range(10):
    if number % 2 == 0:
        print(f'Number {number} is pair')
    print(f'Number {number} is not pair')

In this case, everytime you find a pair number it will continue and not print the second statement. Inside functions where you need to return something and then the iteration stops, it’s pretty usefull.

def find_the_first_odd_number(sequence_long: int):
    for number in range(sequence_long):
        if number % 2 == 0:
            return number
first_odd = find_the_first_odd_number(10)

If you don’t have a for loop and you want to not do anything, the propper statement is "pass" to continue the flow or break, to stop the process.

Answered By: Jose M. González
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.