Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

python - While loop stops after few cycles

I have a problem with my simple python program. While the loop is getting stuck after 6 cycles every time unless it meets desired conditions before that.

I am new to software development and python. I tried to use pdb to find what's going on, but couldn't figure out how to use it .. yet. Also any suggestions on how to make this code better cleaner etc are welcome.

Here is the code:

import random as rd

random = rd.randint(1,48)
odd = 0
even = 0
low_n = 0
high_n = 0
rdlist = []
condition = False
count = 0

def loop(rdlist):
    global random,odd,even,low_n,high_n 
    for _ in range(6):
        while random not in rdlist:
            rdlist.append(random)
            
            if random % 2 == 1:
                odd += 1
            if random % 2 == 0:
                even += 1
            if random <= 24:
                low_n += 1
            if random >= 25:
                high_n += 1  
            
            
        while random in rdlist:
            random = rd.randint(1,48)

def validation():
    global condition
    if odd == even == high_n == low_n == 3:
        condition = True
        rdlist.sort()    
        print(rdlist)

        
    else:
        condition = False
        loop(rdlist)

  
    
loop(rdlist) 
while condition != True:
    validation()
question from:https://stackoverflow.com/questions/65648305/while-loop-stops-after-few-cycles

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...