Battleship, these two are new contacts.
1) generate a random number between A and B:
1 from Import Randint 2 Randint (A, B)
2) Remove the quotes and commas from the list
1 def Print_board (board): 2 for inch Board: 3 Print " ". Join (ROW)
Here is the code
1 fromRandomImportRandint2 3board = []4 5 forXinchRange (5):6Board.append (["O"] * 5)7 8 defPrint_board (board):9 forRowinchBoard:Ten Print " ". Join (ROW) One A Print "Let ' s play battleship!" - Print_board (board) - the defRandom_row (board): - returnRandint (0, Len (board)-1) - - defRandom_col (board): + returnRandint (0, Len (board[0))-1) - +Ship_row =Random_row (board) AShip_col =Random_col (board) at PrintShip_row - PrintShip_col - - forTurninchRange (4): - Print"Turn", turn+1 -guess_row = Int (Raw_input ("Guess Row:")) inguess_col = Int (Raw_input ("Guess Col:")) - to ifGuess_row = = Ship_row andGuess_col = =Ship_col: + Print "congratulations! You sunk my battleship!" - Break the Else: * if(Guess_row < 0orGuess_row > 4)or(Guess_col < 0orGuess_col > 4): $ Print "Oops, that's not even in the ocean."Panax Notoginseng elif(Board[guess_row][guess_col] = ="X"): - Print "You guessed that one already." the Else: + Print "You missed my battleship!" ABoard[guess_row][guess_col] ="X" the Print_board (board) + ifTurn==3: - Print"Game over" $
Progress is a bit slow, need to write more code!
Python Learning Note 1204