Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 10\n"))`

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

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

`if guess2 != number:`
 ` print(name1 + " you have lost try again")`
`else:`
` 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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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')
        continue
    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:
            continue
        else:
            return number
first_odd = find-the_first_odd_number(10)
print(first_odd)

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading