我如何修复在我的Python while循环中出现的’continue not properly in loop’错误?

huangapple go评论175阅读模式
英文:

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

问题

我试图让我的程序这样做,如果答案不在5到10之间,它将重新写入输入,但是当我尝试使用while Truecontinue时,它显示continue没有正确在循环内。只有当我去掉行前的空格时,它才会修复,但这会在else:行上创建一个“语法错误”。
我无法将“continue”向前移动一个或多个空格,也无法将其向后移动任何空格。对于“while True”行也是如此。

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'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(&quot;What is your name? &quot;)
while True:
    guess2 = int(input(&quot;Pick a number between 5 and 10\n&quot;))

if guess2 &lt;5 or guess2 &gt;10:
    print(&quot;this isn&#39;t a number between 5 and 10 please try again.&quot;) 
    continue

else:
    print(&quot;\nYour guess was&quot;, guess2)
    print(&quot;the computer guessed&quot;, number)

if guess2 != number:
    print(name1 + &quot; you have lost try again&quot;)
else:
    print(name1 + &quot; you have Won the game try again if you want to&quot;)
print(&quot;\nNAME:&quot;,name1,&quot;\nUSER GUESS:&quot;,guess2,&quot;\nCOMPUTER CHOICE:&quot;,number,&quot;\nGAME OVER&quot;)

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

答案1

得分: 0

Continue是一个用于控制流程的语句,在迭代中用于继续下一个值的遍历。在这种情况下,它被用在一个for循环中。

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

在这种情况下,每当找到一个偶数时,它会继续执行,并且不会打印第二个语句。在需要返回某些内容然后停止迭代的函数内部,这非常有用。

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)

如果你没有一个for循环并且想什么都不做,合适的语句是"pass"来继续流程,或者"break"来停止这个过程。

英文:

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&#39;Number {number} is pair&#39;)
        continue
    print(f&#39;Number {number} is not pair&#39;)

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.

huangapple
  • 本文由 发表于 2023年5月28日 20:27:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定