英文:
How can I fix 'continue not properly in loop' error in my Python while loop?
问题
我试图让我的程序这样做,如果答案不在5到10之间,它将重新写入输入,但是当我尝试使用while True
和continue
时,它显示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("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.
答案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'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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论