1

I am trying to create a loop to play a game.

I want to match if a tuple returns value as provided, the result should be confess or Don't confess. Lets call them payoff. But in the following test code it returns (0, 0) as confess which was not in the original list returns Confess.

Dont confess: (-1, -1) 
Confess : (-10, 0)
Confess : (0, -10)
Dont Confess: (-10, -10)


dntCon=(-1, -1) 
conf=(-10, 0)
confess=(0, -10)
DntConff=(-5, -5)

import random 
from random import shuffle
from random import sample


x = [-1, -10, 0, -5]
y = [-1, 0, -10, -5]

#print(sample(list, len(list)))

for m in range(0, 3): 
    x=random.sample(x, len(x))
    y=random.sample(y, len(y))
    #print(x, y)

    for i in x:
        #print(i)
        for j in y:
            #print(i, j)
            if(i ==-1 & j==-1 ):
                print(i, j, "ncoo")
                #print( i, j,'')
            elif (i==-10 & j==0): 
                print(i, j, 'Confess')
            elif (i==0 & j==-10):
                print(i, j, 'Confess')
            elif (i==-10 & j==-10):
                print(i, j, 'ncoo')
            else:
                "not met"

Results

(0, 0, 'Confess')
    (-10, -10, 'ncoo')
    (-10, -1, 'ncoo')
    (-1, -1, 'ncoo')
    (0, 0, 'Confess')
    (-10, -10, 'ncoo')
    (-10, -1, 'ncoo')
    (-1, -1, 'ncoo')
    (-10, -1, 'ncoo')
    (-10, -10, 'ncoo')
    (-1, -1, 'ncoo')
    (0, 0, 'Confess')

Why is this loop returning (0, 0) as confess which does not meet my criteria

lpt
  • 931
  • 16
  • 35
  • It could have something to do with your (mis)usage of `&` as the logical and operator. Python uses the literal word `and` for such purposes. – Dillon Davis Feb 15 '19 at 02:27

1 Answers1

1

In python, there is slight difference between '&' and 'and'. Refer this link to check out Difference between 'and' (boolean) vs. '&' (bitwise) in python. Why difference in behavior with lists vs numpy arrays? Try this code out:

Dont_confess: (-1, -1)
Confess : (-10, 0)
Confess : (0, -10)
DontConfess: (-10, -10)


dntCon=(-1, -1) 
conf=(-10, 0)
confess=(0, -10)
DntConff=(-5, -5)

import random 
from random import shuffle
from random import sample


x = [-1, -10, 0, -5]
y = [-1, 0, -10, -5]

#print(sample(list, len(list)))

for m in range(0, 3): 
    x=random.sample(x, len(x))
    y=random.sample(y, len(y))
    #print(x, y)

    for i in x:
        #print(i)
        for j in y:
            #print(i, j)
            if(i ==-1 and j==-1 ):
                print(i, j, "ncoo")
                #print( i, j,'')
            elif (i==-10 and j==0):
                print(i, j, 'Confess')
            elif (i==0 and j==-10):
                print(i, j, 'Confess')
            elif (i==-10 and j==-10):
                print(i, j, 'ncoo')
            else:
                "not met"
Kamal Aujla
  • 327
  • 2
  • 10