dimanche 1 mars 2015

python while and if with pairs of conditions

I can't figure out why this is not working, I've looked at how the "if any([])" syntax is used but in my case I have conditions that come in pairs.


I am trying to generate a random sequence of arrows where all combinations are allowed except the negation of the last arrow (so if the first arrow is L, then the next arrow cannot be R). The code should be keeping Ch2 = 0 if a disallowed sequence occurs and hence in the while loop, otherwise it should set Ch2 = 1 and then I can write code to move on to the next arrow in the sequence.


Also I'm sure there is a better way to do this, but I am just learning Python.



Arrow_Array = ['L.png', 'R.png', 'U.png', 'D.png']
Ch2 = 0

Choice1 = random.choice(Arrow_Array)

while Ch2 != 1:
Choice2 = random.choice(Arrow_Array)
if any([Choice1 == 'L.png' and Choice2 == 'R.png', Choice1 == 'R.png' and Choice2 == 'L.png', Choice1 == 'U.png' and Choice2 == 'D.png', Choice1 == 'D.png' and Choice2 == 'U.png']):

Ch2 = 0
else:
Ch2 = 1

Aucun commentaire:

Enregistrer un commentaire