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

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

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

问题

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

  1. import random
  2. number = random.randint(5,10)
  3. name1 = input("What is your name? ")
  4. while True:
  5. guess2 = int(input("Pick a number between 5 and 10\n"))
  6. if guess2 < 5 or guess2 > 10:
  7. print("this isn't a number between 5 and 10 please try again.")
  8. continue
  9. else:
  10. print("\nYour guess was", guess2)
  11. print("the computer guessed", number)
  12. if guess2 != number:
  13. print(name1 + " you have lost try again")
  14. else:
  15. print(name1 + " you have Won the game try again if you want to")
  16. 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.

  1. import random
  2. number = random.randint(5,10)
  3. name1 = input(&quot;What is your name? &quot;)
  4. while True:
  5. guess2 = int(input(&quot;Pick a number between 5 and 10\n&quot;))
  6. if guess2 &lt;5 or guess2 &gt;10:
  7. print(&quot;this isn&#39;t a number between 5 and 10 please try again.&quot;)
  8. continue
  9. else:
  10. print(&quot;\nYour guess was&quot;, guess2)
  11. print(&quot;the computer guessed&quot;, number)
  12. if guess2 != number:
  13. print(name1 + &quot; you have lost try again&quot;)
  14. else:
  15. print(name1 + &quot; you have Won the game try again if you want to&quot;)
  16. 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循环中。

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

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

  1. def find_the_first_odd_number(sequence_long: int):
  2. for number in range(sequence_long):
  3. if number % 2 == 0:
  4. continue
  5. else:
  6. return number
  7. first_odd = find_the_first_odd_number(10)
  8. 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.

  1. for number in range(10):
  2. if number % 2 == 0:
  3. print(f&#39;Number {number} is pair&#39;)
  4. continue
  5. 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.

  1. def find_the_first_odd_number(sequence_long: int):
  2. for number in range(sequence_long):
  3. if number % 2 == 0:
  4. continue
  5. else:
  6. return number
  7. first_odd = find_the_first_odd_number(10)
  8. 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:

确定