首页 > 解决方案 > 为什么我的简单程序在运行后没有结束?

问题描述

这是我的小程序:从一组字母中创建所有可能的字符串。

问题:我希望它在获得列表后停止,PyCharm 等待我按下停止按钮。

import random

liste = []
char_list = ['a', 'b', "c", "d"]

while True:

    random.shuffle(char_list)
    n = ''.join(char_list)

    if n in liste:
        continue
    elif n not in liste:
        print(''.join(char_list))
        liste.append(n)
    else:
        break

为什么这个程序在给出列表后没有停止?

标签: python

解决方案


它永远不会因为你的条件而停止。

    # If n is in liste...
    if n in liste:
        continue
    # Otherwise, if n not in liste...
    elif n not in liste:
        print(''.join(char_list))
        liste.append(n)
    # Will never happens, because either n is or is not in liste...
    else:
        break

推荐阅读