Python Learning Note 1204

Source: Internet
Author: User

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

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.