This is a working code of the ex 16. But I am confused why this is working..
We have never declared a variable turn, but it is still accessible when we jump out from to for loop after 4 turns because "Game Over" is printed, eg. turn is declared and has a value of 3.
So in python when we write
for turn in range(4): are we declaring turn as a global variable?
# Write your code below! for turn in range(4): print "Turn", turn + 1 # Print (turn + 1) here guess_row = int(raw_input("Guess Row:")) guess_col = int(raw_input("Guess Col:")) if guess_row == ship_row and guess_col == ship_col: print 'Congratulations! You sank my battleship!' else: if guess_row not in range(5) or guess_col not in range(5): print "Oops, that's not even in the ocean." elif board[guess_row][guess_col] == 'X': print "You guessed that one already." else: print "You missed my battleship!" board[guess_row][guess_col] = "X" if turn == 3: print "Game Over" print_board(board)