[英]python looping with functions break and continue outside loop
我是编程新手,所以我正在投标。 在这一点上,我正在练习功能。 在下面的代码中,break和continue在循环之外,我看不到原因。 我尝试了不同的方法,但我要做的唯一一件事就是最后的代码块。 为什么在这里中断并继续循环?
随机导入
again = 'y'
while again == "y" :
def main():
print "gues a number between 0 - 10."
nummer = random.randint(1,10)
found = False
while not found:
usergues = input("your gues?")
if usergues == nummer:
print 'Your the man'
found = True
else:
print 'to bad dude try again'
main()
again = raw_input('would you like to play again press y to play again press n yo exit')
if again == 'n':
break #here it says it is outside the loop
elif again != 'y':
print 'oeps i don\'t know what you mean plz enter y to play again or n to exit'
else:
continue #this is outside loop as well
#main()
因为您是编程新手,所以我在回答中也会得到一些基本提示。
无限循环
您尝试通过首先again = 'y'
设置again = 'y'
来开始无限循环,然后使用此变量来评估while循环。 因为您没有更改y的值,所以最好不要使用变量来创建此无限循环。 相反,请尝试以下操作:
while True:
(some code)
定义功能
您正在while循环中定义函数main()
。 据我所知,这没有用。 只需忽略第一个while循环即可。 如果定义一个函数,则该函数是永久性的(很像一个变量),因此无需每次都重新定义它。 使用代码,您甚至都不会调用该函数,因为您永远不会结束第一个循环。
继续/中断
该错误是不言自明的,但是在这里我们开始。 如果您将结束第一个循环(在这种情况下不会结束),那么下一步是调用函数main()
。 这将生成一个数字并使用户猜对,直到正确为止。 发生这种情况时,您将退出该函数(并循环执行)。
接下来,您询问用户是否要再次玩。 这只是一个输入语句。 您将答案存储在变量“再次”中。 您使用if语句(请注意,这不是循环!)检查答案是什么。 您希望用户在键入“ y”后再次播放,因此您可以使用以下命令来代替again != 'y'
使用again != 'y'
:
if again == 'y':
main() # you call the function to play again
如果输入了'n',则要退出脚本,而不要通过键入break
退出脚本,因为您不在循环中,而只是在if语句中。 您可以不输入任何内容,这只会超出if语句。 因为if之后没有任何内容,所以您将退出脚本。 您也可以使用exit()
,它将立即退出脚本。
最后,如果这两件事都没有回答,您想重复这个问题。 您可以将if语句放入循环中。 您可以(如果需要)使用中断并在执行此操作时继续,但是您大多希望避免这两个。 这是一个例子:
while True:
again = raw_imput('y for again or n to stop')
if again == 'y':
main()
exit() # use this if you don't want to ask to play again after the 2nd game
elif again == 'n':
print('bye!')
exit()
# no need for an 'else' this way
# every exit() can be replaced by a 'break' if you really want to
基本中断/继续使用
最后,这是break
和continue
一些基本用法。 人们通常倾向于避免使用它们,但是很高兴知道他们的所作所为。
使用break
将退出您当前所在的最内部的循环,但是显然,您只能在循环内部使用它(for循环或while循环)。
不管接下来的代码是什么,使用continue
将立即重新启动您当前处于的最内部循环。 同样,仅在循环内部可用。
一切都在一起
import random
again = 'y'
def main():
print ("gues a number between 0 - 10.")
nummer = random.randint(1,10)
found = False
while not found:
usergues = input("your gues?")
if usergues == nummer:
print ('Your the man')
found = True
else:
print ('to bad dude try again')
main()
while True:
again = input('would you like to play again press y to play again press n yo exit')
if again == 'n':
print ('bye!')
exit() # you could use break here too
elif again == 'y':
main()
exit() # you can remove this if you want to keep asking after every game
else:
print ('oeps i don\'t know what you mean plz enter y to play again or n to exit')
希望对您有帮助!
您可能要参考教学材料,因为您似乎误解了功能的一般用途和逻辑顺序。
您的功能应在外部范围内,例如:
def main():
again = 'y'
while again == "y" :
您的问题再次需要缩进while循环中:
while again == "y":
[snip]
again = raw_input('would you like to play again press y to play again press n to exit')
if again == 'n':
break #here it says it is outside the loop
elif again != 'y':
print 'oops i don\'t know what you mean plz enter y to play again or n to exit'
else:
continue #this is outside loop as well
else: continue
因为循环已经结束,所以else: continue
是不必要的。
但是,这只问了一次问题,您可能希望在while循环中这样做。 您也不需要在外部while循环中again == "y"
检查again == "y"
,因为您在这里控制流程:
while True:
[snip]
again = raw_input("would you like to play again press y to play again press n to exit")
while again not in ('y', 'n'):
again = raw_input("oops i don't know what you mean plz enter y to play again or n to exit")
if again == 'n':
break
我建议不要使用裸露的input()
因为可以执行任何代码,接收字符串并将其强制转换为int
是安全的(而且您可能会进行一些错误检查):
usergues = int(raw_input("your guess?"))
全部放在一起看起来像:
def main():
while True:
print "guess a number between 1 - 10."
nummer = random.randint(1,10)
found = False
while not found:
usergues = int(raw_input("your guess?"))
if usergues == nummer:
print 'You're the man'
found = True
else:
print 'Too bad dude try again'
again = raw_input('would you like to play again press y to play again press n to exit')
while again not in ('y', 'n'):
again = raw_input('oops i don\'t know what you mean plz enter y to play again or n to exit')
if again == 'n':
break
main()
您的循环和def
都一团糟,您想要更多类似的东西:
import random
again = 'y'
while again == "y" :
print "gues a number between 0 - 10."
nummer = random.randint(1,10)
found = False
while not found:
usergues = input("your gues?")
if usergues == nummer:
print 'Your the man'
found = True
else:
print 'to bad dude try again'
while True:
again = raw_input('would you like to play again press y to play again press n to exit')
if again == 'n':
break
elif again != 'y':
print 'oeps i don\'t know what you mean plz enter y to play again or n to exit'
else:
break
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.