mardi 3 juillet 2018

Not getting correct result,result should not contain same string but it isn't happening.What conditions do i need to pass for if?

My code is having a problem: The result should be like this k kcnb h hklh k kb But it may vary after each execution This is the code (Sorry for the lengthy code) and If you could run the code you will know what I'm talking about:

 from random import randint,choice
 val=[]
 a='hklh kcnb jvbj kb kbk'
 a=a.split()
 b="khk"
 p=[]
 val=[]

 for s in b:
     for d in a:
         if s in d[0]:
             val.append(d)
             check=d
     if len(val)==1:
         p.append((s,val[0]))

     elif len(val)>1:
         v=randint(0,len(val)-1)
         ff=val[v]
         p.append((s,ff))
         check=ff
     else:
         p.append((s,''))
     for ad in p:
         if s==ad[0] and check==ad[1] and len(p)!=1 and len(val)!=1:
             e=choice([i for i in range(0,len(val)-1) if i!=v])
             p.pop()
             p.append ((s,val[e]))
     val=[]
 print(p)
 output = "\n".join(["{key} {val}".format(key=t[0],val=t[1]).strip() for t in p]) 
 print(output)

And The code I am using to exclude a number from randint and the if condition.

    if s==ad[0] and check==ad[1] and len(p)!=1 and len(val)!=1:
            e=choice([i for i in range(0,len(val)-1) if i!=v])
            p.pop()
            p.append 

And I am new to stackoverflow, so if there is any mistakes,sorry!

Aucun commentaire:

Enregistrer un commentaire