Skip to main content
 首页 » 编程设计

Python while循环没有中断

2025年05月04日15sxdcgaq8080

我是一名新程序员,我正在尝试制作一个基本的密码生成器。但是我一直遇到这个问题,我的 while 循环永远不会中断。

l1 = 'q w e r t y u i o p a s d f g h j k l z x c v b n m 1 2 3 4 5 6 7 8 9 0' 
l2 = l1.split() 
 
 
def genpass(n): 
    x = 0       if x == 0: 
        password = '' 
    if n < 100: 
        while n > x: 
            password = password + random.choice(l2) 
            x + 1 
        print(password) 
    else: 
        print 'Sorry, too long' 

有人能告诉我我做错了什么吗?谢谢。

请您参考如下方法:

永远不要在此处更改 n 或 x:

while n > x: 
    password = password + random.choice(l2) 
    x + 1 

因此,如果条件最初为 True,它将始终保持 True 并无限循环。需要做 x = x + 1

顺便说一下,这正是 Pylint 会为您捕获的错误类型。