Python list Syntax Learning

Source: Internet
Author: User
Created: List = [5,7,9]
Value and change: list[1] = list[1] * 5
End of List insert: List.append (4)
Subtract the No. 0 value and return the value of the No. 0 value: List.pop (0)
Remove the No. 0 value without returning a value: Del (list[0])
Remove a specific value: List.remove (35)

function functions:
No parameters: Def function ():
One parameter: def function (x):
2 Parameters: def function (y):
Any of the parameters: Def add_function (*args):

Function Range:
One parameter: Range (n) starts from No. 0 digit n digit
Two parameters: Range (m,n) starts from the first m bit to the n-1 bit, with an increment interval of 1
Three parameters: Range (m,n,i) starts from the first m bit to the n-1 bit, with an increment interval of I
For item in list: And for I in range (len (list)): equivalent

Use separator as the interval output for elements in list: print Separator.join (list)
For example: List = [' A ', ' B ', ' C ', ' d '] The general print List will output: [' A ', ' B ', ' C ', ' d '].
and print "". Join (list) outputs: a B c D (must be double quotes, single double quotes do not work)

Accept keyboard Input:
guess_row = Int (raw_input ("Guess row:"))

Here is a small program that you write yourself: Generate a square and a random position, and ask the player to guess where the generated position is

From random import randintdef creat_board (length): board = [] for i in range (length): Board.append ([' O '] * le Ngth) return boarddef print_board (x): For row in X:print "". Join (ROW) def random_row (board): Return Randi NT (0, Len (board)-1) def random_col (board): Return Randint (0,len (board[0))-1) length = Int (raw_input ("Enter board ' s Le Ngth: ")) board = creat_board (length) print_board (board) turns = Int (raw_input (" Enter turns you want to play: "))) for turn I N Range (turns): Ship_row = random_row (board) Ship_col = random_col (board) print "This is" + str (turn + 1) + "th Time to guess: "guess_row = Int (raw_input (" Enter the row, guess: ")) Guess_col = Int (raw_input (" Enter the col you        Guess: ")) if Guess_row = = Ship_row and Guess_col = = Ship_col:print" You win! " Break Else:if (Guess_row < 0 or Guess_row > len (Board)-1) or (Guess_col < 0 or Guess_col > len (bo    ARD)-1): print "Incorrect input!"        if turn = = turns-1: print "turns out!"            Elif Board[guess_row][guess_col] = = ' X ': print "You have guessed it already!"        if turn = = turns-1: print "turns out!"            Else:print "You guess wrong!" Board[guess_row][guess_col] = ' X ' print_board (board) if turn = = turns-1: print "Tur NS out! "

Error:
1. Create board function forget to return a board, so it has been empty, resulting in the subsequent operations are out of bounds;
2. When generating random positions, the naming of the row and Col is always the same as the name of the generated function (random_row= Random_row (board)), causing typeerror: ' int ' object is not callable error.

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.