Use "and" many times in loop in Python -


i tried use and in while loop seems not work well. example:

while (a1==0) , (a2==0) , (a3==0) , (a4==0): 

with one, seems end loop first , second and, , not conditions satisfied. wrong way use and while loop?

below code:

import random b1=0 b2=0 b3=0 b4=0 = random.randrange(1,5) while all([b1==0, b2==0, b3==0, b4==0]):     if != b1:         b1 =         print(b1)     = random.randrange(1,5)     if (a!= b2) , (a!= b1):         b2 =         print(b2)     = random.randrange(1,5)     if (a!= b3) , (a!= b2) , (a!= b1):         b3=a         print(b3)     = random.randrange(1,5)     if (a!= b4) , (a!= b3) , (a!= b2) , (a!= b1):         b4=a         print(b4) 

i want print 1,2,3,4 in random order prints 2 or 3 of them.

in spite of fact short-circuiting (and stops evaluation @ first falsy result), there's more concise way write using all:

while all([a1 == 0, a2 == 0, a3 == 0, a4 == 0]):     # work here 

while of values being generated inside of list being evaluated (and hence not short-circuiting), all return false on first falsy occurrence.


since you've added code you're using, expressions never true on second iteration. random.randrange(1, 5) only generate values between 1 , 5 exclusive. reduce start value 0 have chance of generating 0.


since you've clarified want do, need generate 4 random values, , check if match against set containing values want.

result = [] expected = {1, 2, 3, 4} while set(result) != expected:     # surely there's better method generate this...     result = [random.randint(1, 4), random.randint(1, 4),               random.randint(1, 4), random.randint(1, 4)] print result 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -