Create: list = [5, 7, 9]
Value and value change: list [1] = list [1] * 5
Insert at the end of the list: list. append (4)
Remove the 0th values and return the value of The 0th values: list. pop (0)
Remove the 0th values but no value is returned: del (list [0])
Remove a specific value: list. remove (35)
Function:
No parameter: def function ():
One parameter: def function (x ):
Two parameters: def function (y ):
Any parameter: def add_function (* args ):
Function range:
One parameter: range (n) starts from 0th digits and counts n digits
Two parameters: range (m, n) starts from the MTH bit to the n-1 bit, and the increment interval is 1.
Three parameters: range (m, n, I) starts from the MTH bit to the n-1 bit, and the incremental interval is
For item in list: equivalent to for I in range (len (list ):
Use separator as the interval output for the elements in the list: print separator. join (list)
For example: list = ['A', 'B', 'C', 'D'] General print list will output: ['A', 'B ', 'C', 'D'].
Print "". join (list) will output: a B c d (it must be double quotation marks, and single double quotation marks do not work)
Accept keyboard input:
Guess_row = int (raw_input ("Guess Row :"))
The following is a self-written small program: generates a matrix and a random position, which is used by players 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 you want to play :"))
For turn in 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 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! "
Previous mistakes:
1. When I create a board function, I forget to return a board. Therefore, it is always empty, and subsequent operations are out of bounds;
2. when a random position is generated, the row and col names are always the same as the generated function names (random_row = random_row (board), resulting in TypeError: 'int' object is not callable error.