fromRandomImportRandint#Introducing Random FunctionsBoard= [] #to build an empty list forXinchRange (5): Board.append (["O"] * 5) #use the range () method to write a value to the list, each ["0"]*5 is ItemdefPrint_board (board): forRowinchBoard:Print " ". Join (ROW)#output list, remove quotation marks and parentheses, concatenate with spacesPrint "Let ' s play battleship!"Print_board (board)defRandom_row (board):returnRandint (0, Len (board)-1) defRandom_col (board):returnRandint (0, Len (board[0))-1) Ship_row=Random_row (board) Ship_col=Random_col (board)#Everything from this on should go your for loop! #Be sure to indent four spaces! forTurninchRange (4): #Cycle four timesguess_row = Int (Raw_input ("Guess Row:")) #Enter a guess valueguess_col = Int (Raw_input ("Guess Col:")) ifGuess_row = = Ship_row andGuess_col = =Ship_col:Print "congratulations! You sunk my battleship!" Break Else: if(Guess_row < 0orGuess_row > 4)or(Guess_col < 0orGuess_col > 4): Print "Oops, that's not even in the ocean." elif(Board[guess_row][guess_col] = ="X"):#if it has been populated, the x description is a duplicate value Print "You guessed that one already." Else: Print "You missed my battleship!"Board[guess_row][guess_col]="X" #the wrong value is filled with x Print(turn+1) ifTurn==3: Print "Game over"Print_board (board)
Codecademy the nineth unit of the small game, although passed, but feel there is a mishap, need to adjust later, for example, the round is starting from 0, the number of rows entered in the column should be-1, the input value is not empty
Python Codecademy 9 Unit battleship!