为什么我的while循环在函数中使用时不会停止?

时间:2017-12-13 22:23:40

标签: python

好吧所以我要么不理解其他问题中的代码,要么我的问题不同。 这是我的代码:

    import random, math, sys, time

    userinput = 0

    while "quit" != userinput:

        def lol():
            global userinput, why
            why = userinput = input("What the heck are you doing? ")
            print(why, userinput)
            lol()

        lol()

        why = userinput = input("Type: ")

当我在“类型:”中键入“退出”时,程序退出。但是,当我在我的函数中键入“quit”时,它会继续运行。这是为什么?

2 个答案:

答案 0 :(得分:1)

一旦进入lol函数,它就会循环。无论你进入哪里,都不会接受测试,看它是否等于“退出”。

因此,一旦你遵循程序流程,你的问题的答案就会变得清晰:大概你可以从循环中的某个地方调用lol函数,然后无论你输入什么,该函数都会自行调用。该函数无休止地迭代。

答案 1 :(得分:1)

您的代码看起来非常奇怪。另外,你实际上在循环中有功能,而不是相反。

你还有一个函数lol指向自身,所以它会一直重复。

为您清除了代码,同时删除了“为什么”,如果您需要,可以添加全局化。

 self.MyOfferCollectionView.collectionViewLayout = [[CustomImageFlowLayout alloc] init];