# Chapter 6 : Arrays¶

## Example 6.1, Page No 134¶

In :
iArray=range(5)

for x in range(5):
iArray[x]=0


## Example 6.2, Page No 134¶

In :
iArray=range(5)

for x in range(5):
iArray[x]=x

for x in range(5):
print "The value of iArray index "+str(x)+"is "+str(x)

The value of iArray index 0is 0
The value of iArray index 1is 1
The value of iArray index 2is 2
The value of iArray index 3is 3
The value of iArray index 4is 4


## Example 6.3, Page No 136¶

In :
iIndex = -1
iArray=range(5)

for x in range(5):
iArray[x]=(x+5)

while (iIndex < 0 or iIndex >4):
iIndex=int(raw_input("Enter a valid index (0-4): "))

print "The value of index "+str(iIndex)+" is "+str(iArray[iIndex])

Enter a valid index (0-4): 4
The value of index 4 is 9


## Example 6.4, Page No 137¶

In :
cArray = range(5)
cName="Olivia"

print "\nCharacter array not initialized:\n"

for x in range(5):
print "Element "+str(x)+"'s Contents are ",cArray[x]

print "\nInitialized character array: "
for x in range(6):
print cName[x],

Character array not initialized:

Element 0's Contents are  0
Element 1's Contents are  1
Element 2's Contents are  2
Element 3's Contents are  3
Element 4's Contents are  4

Initialized character array:
O l i v i a


## Example 6.5, Page No 138¶

In :
iFound = -1
iArray=range(5)

for x in range(5):
iArray[x]=(x+x)

iValue=int(raw_input("Enter value to search for: "))

for x in range(5):
if (iArray[x]==iValue):
iFound=x
break

if(iFound > -1):
print "I found your search value in element ",iFound
else:
print "Sorry, your search value was not found"

Enter value to search for: 8
I found your search value in element  4


## Example 6.6, Page No 141¶

In :
iTwoD=[[0 for j in range(3)]for i in range(3)]
for x in range(3):
for y in range(3):
iTwoD[x][y]=(x+y)
for x in range(3):
for y in range(3):
print "iTwoD["+str(x)+"]["+str(y)+"]=",iTwoD[x][y]

iTwoD= 0
iTwoD= 1
iTwoD= 2
iTwoD= 1
iTwoD= 2
iTwoD= 3
iTwoD= 2
iTwoD= 3
iTwoD= 4


## Example 6.7, Page No 143¶

In :
iTwoD=[[1,2,3],[4,5,6],[7,8,9]]
iFoundAt=[0,0]

iValue=0
iFound=0

iValue=int(raw_input("Enter your search value: "))

for x in range(3):
for y in range(3):
if(iTwoD[x][y]==iValue):
iFound=1
iFoundAt=x
iFoundAt=y
break

if (iFound==1):
print "Found value in iTwoD ["+str(iFoundAt)+"]["+str(iFoundAt)+"]"
else:
print "Value not found"

Enter your search value: 7
Found value in iTwoD 


## Example 6.8, Page No 145¶

In :
board=range(9)
cWhoWon=' '
iCurrentPlayer=0

def displayBoard():
print "\n\t|\t|\n"
print "\t|\t|\n"
print ""+str(board)+"\t|"+str(board)+"\t|"+str(board)+"\n"
print "--------|-------|--------\n"
print ""+str(board)+"\t|"+str(board)+"\t|"+str(board)+"\n"
print "--------|-------|--------\n"
print "\t|\t|\n"
print ""+str(board)+"\t|"+str(board)+"\t|"+str(board)+"\n"
print "\t|\t|\n"

def verifySelection(iSquare,iPlayer):
if(board[iSquare-1]==' ' and (iPlayer==1 or iPlayer==0)):
board[iSquare-1]='X'
return 0
elif(board[iSquare-1]==' ' and iPlayer==2):
board[iSquare-1]='0'
return 0
else:
return 1

def checkForWin():
global cWhoWon
if(board=='X' and board=='X' and board=='X'):
cWhoWon='X'
elif(board=='X' and board=='X' and board=='X'):
cWhoWon='X'
elif(board=='X' and board=='X' and board=='X'):
cWhoWon='X'
elif(board=='X' and board=='X' and board=='X'):
cWhoWon='X'
elif(board=='X' and board=='X' and board=='X'):
cWhoWon='X'
elif(board=='X' and board=='X' and board=='X'):
cWhoWon='X'
elif(board=='X' and board=='X' and board=='X'):
cWhoWon='X'
elif(board=='X' and board=='X' and board=='X'):
cWhoWon='X'
elif(board=='0' and board=='0' and board=='0'):
cWhoWon='0'
elif(board=='0' and board=='0' and board=='0'):
cWhoWon='0'
elif(board=='0' and board=='0' and board=='0'):
cWhoWon='0'
elif(board=='0' and board=='0' and board=='0'):
cWhoWon='0'
elif(board=='0' and board=='0' and board=='0'):
cWhoWon='0'
elif(board=='0' and board=='0' and board=='0'):
cWhoWon='0'
elif(board=='0' and board=='0' and board=='0'):
cWhoWon='0'
elif(board=='0' and board=='0' and board=='0'):
cWhoWon='0'

if(cWhoWon!=' '):
if(cWhoWon=='X'):
print "\nX Wins!\n"
return

if(cWhoWon=='0'):
print "\n0 Wins!\n"
return
else:
return

for x in range(9):
if(board[x]!=' '):
catTotal=catTotal+1

if(catTotal==9):
cWhoWon='C'
print "\nCAT Game!\n"
return
iSquareNum=0

for x in range(8):
board[x]=' '

displayBoard()

while(cWhoWon==' '):
print "\n"+cWhoWon+"\n"

if(iCurrentPlayer==1 or iCurrentPlayer==0):
print "\nPLAYER X\n"
iSquareNum=int(raw_input("Enter an available square number (1-9): "))

if(verifySelection(iSquareNum,iCurrentPlayer)==1):
iCurrentPlayer=1
else:
iCurrentPlayer=2
else:
print "\nPLAYER 0\n"
iSquareNum=int(raw_input("Enter an available square number (1-9): "))

if(verifySelection(iSquareNum,iCurrentPlayer)==1):
iCurrentPlayer=2
else:
iCurrentPlayer=1
displayBoard()
checkForWin()


	|	|

|	|

| 	|

--------|-------|--------

| 	|

--------|-------|--------

|	|

| 	|8

|	|

PLAYER X

Enter an available square number (1-9): 1

PLAYER 0

Enter an available square number (1-9): 2

|	|

|	|

X	|0	|

--------|-------|--------

| 	|

--------|-------|--------

|	|

| 	|8

|	|

PLAYER X

Enter an available square number (1-9): 4

PLAYER 0

Enter an available square number (1-9): 3

|	|

|	|

X	|0	|0

--------|-------|--------

X	| 	|

--------|-------|--------

|	|

| 	|8

|	|

PLAYER X

Enter an available square number (1-9): 7

PLAYER 0

Enter an available square number (1-9): 5

|	|

|	|

X	|0	|0

--------|-------|--------

X	|0	|

--------|-------|--------

|	|

X	| 	|8

|	|

X Wins!