Python list Syntax learning (with example)

Source: Internet
Author: User
Tags python list
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
Copy CodeThe code is as follows:


From random import Randint
def creat_board (length):
board = []
For I in range (length):
Board.append ([' O '] * length)
Return board
def print_board (x):
For row in x:
Print "". Join (Row)
def random_row (board):
Return Randint (0, Len (board)-1)

def random_col (board):
Return Randint (0,len (board[0])-1)

length = Int (raw_input ("Enter board ' s length you:"))
board = creat_board (length)
Print_board (board)
turns = Int (raw_input ("Enter turns want to play:"))
For turn in range (turns):
Ship_row = Random_row (board)
Ship_col = Random_col (board)
Print "This was" + STR (turn + 1) + "th time to guess:"
guess_row = Int (raw_input ("Enter The row You 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 (Board)-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 "Turns out!"


Mistakes that have been made:
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 generated function name (Random_row=random_row (board)), resulting in typeerror: ' int ' object is not callable error.
  • 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.