strange while statement behaviour?

后端 未结 2 1268
有刺的猬
有刺的猬 2021-01-26 08:18

I cannot figure out why the following statements dont work.

randomKey = random.choice(list(topic.keys()))
randomValue = random.choice(topic[randomKey])

current          


        
2条回答
  •  不要未来只要你来
    2021-01-26 08:49

    else, when used with while, runs after the while expression evaluates to a falsy value if the while loop finishes by the expression being false, instead of being broken out of by a break statement (or execution leaving the function via a return or raise-ing an exception). Your while condition in your second example must fail, so there's no opportunity for a break to occur, the function to return or an exception to be thrown, so the else statements will always run.

    docs for while

提交回复
热议问题