Not Equal to Each Other?

Juho Schultz juho.schultz at helsinki.fi
Fri Nov 4 08:16:26 EST 2005


ale.of.ginger at gmail.com wrote:
> How do I 'define' set?  Is there something to include (like import
> random)?
> 
set is a built-in type in Python 2.4
If you use 2.3 you can use the sets module with "import sets"


> while (choice == 3) and len(set(cellboard[0:8]))==len(cellboard[0:8]):
>         # DEFINE TWO RANDOM VARIABLES (ONE FOR ARRAY, ONE FOR NUMBER
> VALUE)
>         solvingrandom = random.randint(1,9)
>         cellboardrandom = random.randint(0,8)
>         set(cellboard[0:8])
> 
>         # CHECK TO MAKE SURE THE RANDOMLY ASSIGNED CELL DOES NOT HAVE A
> VALUE
>         if (cellboard[cellboardrandom] is not ('1' or '2' or '3' or '4'
> or '5' or '6' or '7' or '8' or '9')):
>             cellboard[cellboardrandom] = solvingrandom
> 
> The above is my code (right now it will only work for the first row's
> numbers).  Anything else I need to add?
> 
Simplify your code a bit:

'2' is not ('1' or '2' or '3' or '4' or '5' or '6' or '7' or '8' or '9')
evaluates to True
'1' is not ('1' or '2' or '3' or '4' or '5' or '6' or '7' or '8' or '9')
evaluates to False
Somehow I do not believe you want that behavipur.

If cellboard contains characters, you could use:
if (cellboard[cellboardrandom] not in '123456789')

for integers, the following should work:
if not (1 <= cellboard[cellboardrandom] <= 9)

Using None to code empty cells, you could even have:
if (cellboard[cellboardrandom] is None)



More information about the Python-list mailing list