012rev3 - 10 months ago 43
Python Question

# How to use chained not equal comparisons?

I have this code, but fail. I want different value.

Here is a table of inputs and desired outputs.

0, 1, 2 = yes

1, 2, 0 = yes

0, 1, 1 = fail

1, 1, 1 = fail

``````import random
def numberValue():
a1 = random.randrange(0,3)
a2 = random.randrange(0,3)
a3 = random.randrange(0,3)
a1 != a2 != a3 # How can use this?
print "Value: ", a1, ",", a2, ",", a3
numberValue()
``````

I'm guessing you're trying to generate three unique numbers? You could try something like this:

``````import random
def numberValue():
#generate unique a1
a1 = random.randrange(0,3)

#generate unique a2
a2 = random.randrange(0,3)
while(a2 == a1):
a2 = random.randrange(0,3)

#generate unique a3
a3 = random.randrange(0,3)
while(a3 == a2 or a3 == a1):
a3 = random.randrange(0,3)

print "Value: ", a1, ",", a2, ",", a3
numberValue()
``````

If you're trying to simply detect if the three numbers are unique, you can try manually checking them (for simplicity):

``````import random
def numberValue():
a1 = random.randrange(0,3)
a2 = random.randrange(0,3)
a3 = random.randrange(0,3)

if(a1 != a2 and a1 != a3 and a2 != a3):
print "Yes"
else:
print "No"

print "Value: ", a1, ",", a2, ",", a3
numberValue()
``````