python循环与函数break并继续外部循环

时间:2015-11-15 14:51:20

标签: python function while-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()

3 个答案:

答案 0 :(得分:1)

因为你是编程新手,我也会在答案中得到一些基本提示。

INFINITE LOOP

您试图通过首先设置again = 'y'来启动无限循环,然后使用此变量来评估while循环。因为您没有更改y的值,所以最好不要使用变量来创建此无限循环。相反,试试这个:

while True:
    (some code)

循环中的定义功能

您在while循环中定义了函数main()。据我所知,这是没有用的。只需省略第一个while循环。如果定义一个函数,它是永久的(很像一个变量),所以不需要每次都重新定义它。使用您的代码,您甚至无法调用该函数,因为您永远不会结束第一个循环。

CONTINUE / BREAK NOT LOOP

这个错误非常自我解释,但我们走了。如果你要结束第一个循环(在这种情况下,你赢了),接下来你要做的就是调用函数main()。这将生成一个数字并让用户猜测它直到他做对了。当发生这种情况时,你就会离开那个功能(并循环)。

接下来,您询问用户是否想再次播放。这只是一个输入语句。您将答案再次存储在变量“中”。你用if语句检查(注意这不是循环!)答案是什么。您希望用户在键入“' y”时再次播放,因此您可以使用以下内容代替使用again != 'y'

if again == 'y':
    main()  # you call the function to play again

如果' n'输入后,您要退出脚本,而不是键入break,因为您不在循环中,只是在if语句中。您可以输入任何内容,这将只是if-statement。因为在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

基本支出/续续使用

最后,这是breakcontinue的一些基本用法。人们通常倾向于避开它们,但很高兴知道它们的作用。

使用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')

我希望我能帮到你!

答案 1 :(得分:0)

您可能需要参考教学材料,因为您似乎误解了功能的一般目的和逻辑的顺序。
你的功能应该在外部范围,例如:

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是不必要的,因为你处于循环的最后 但是,这只会问一次这个问题,你可能想要在while循环中使用它。你也不需要检查外部while循环中的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()

答案 2 :(得分:0)

你循环,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