Python Codecademy 9 Unit battleship!

Source: Internet
Author: User

 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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.