简体   繁体   中英

While Loop not working in Python

I am struggling on this while loop in python I am trying to make the program continue to repeat itself until it output an lower,upper and digit but it ends as I run the program

import random

num = 3
print(num)

message=""

for n in range(num):
    while message =="":
        newnum= random.randint(48,122)           
        if newnum > 47 and newnum < 58:
            val=chr(newnum)
            val = newnum
            message +=chr(val)

        elif newnum > 64 and newnum <91:
            val=chr(newnum)
            val = newnum
            message +=chr(val)

        elif newnum > 97 and newnum < 123:
            val=chr(newnum)
            val = newnum
            message +=chr(val)    

        else:
            newnum -= random.randint(7,10)
            val=chr(newnum)
            val = newnum
            message +=chr(val)

if (any(x.isupper() for x in message) and any(x.islower() for x in message)and any(x.isdigit() for x in message)):
    print(message)
else:
    message =""

After the first iteration your message will be != of '', because you will set it to a chr(val). Thus it will never re-enter in the while loop again and it will just finish the for loop.

I think your problem is in wrong indention. Try to use

for n in range(num):
    while message =="":
        newnum= random.randint(48,122)           
        if newnum > 47 and newnum < 58:
            val=chr(newnum)
            val = newnum
            message +=chr(val)

        elif newnum > 64 and newnum <91:
            val=chr(newnum)
            val = newnum
            message +=chr(val)

        elif newnum > 97 and newnum < 123:
            val=chr(newnum)
            val = newnum
            message +=chr(val)    

        else:
            newnum -= random.randint(7,10)
            val=chr(newnum)
            val = newnum
            message +=chr(val)

        if (any(x.isupper() for x in message) and any(x.islower() for x in message) and any(x.isdigit() for x in message)):
            print(message)
        else:
            message =""

Your problem is mainly ident and the while condition, take a look at my code.

import random

num = 3
print(num)

message=""
end = False #set a valid condition to end the program
for n in range(num):
#   while message =="":
    while end == False:
        newnum= random.randint(48,122)           
        if newnum > 47 and newnum < 58:
            val=chr(newnum)
            val = newnum
            message +=chr(val)
        elif newnum > 64 and newnum <91:
            val=chr(newnum)
            val = newnum
            message +=chr(val)
        elif newnum > 97 and newnum < 123:
            val=chr(newnum)
            val = newnum
            message +=chr(val)    

        else:
            newnum -= random.randint(7,10)
            val=chr(newnum)
            val = newnum
            message +=chr(val)
        if (any(x.isupper() for x in message) 
             and any(x.islower() for x in message) 
             and any(x.isdigit() for x in message)):

            print(message)
            end=True
#       else:
#           message =""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM