@@ -36,6 +36,16 @@ print(list(range(9, -1, -1)))
3636
3737\[ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0\]
3838
39+
40+
41+ Make the loop print the numbers from 0 to 19 instead of 0 to 9.
42+
43+ ``` python
44+ print " Counting..."
45+ for i in range (0 ,20 ):
46+ print i
47+ ```
48+
3949---
4050
4151Using strings in lists in functions
@@ -50,8 +60,6 @@ def join_strings(words):
5060print join_strings(n)
5161```
5262
53-
54-
5563Using two lists as two arguments in a function
5664
5765``` python
@@ -66,8 +74,6 @@ print join_lists(m, n)
6674
6775\[ 1, 2, 3, 4, 5, 6\]
6876
69-
70-
7177Using a list of lists in a function
7278
7379``` python
@@ -78,9 +84,20 @@ for lst in n:
7884 print item
7985```
8086
87+ ### 內建函數 append\(\)
88+
89+ 把input輸入的內容新增到list
90+
91+ ``` py
92+ hobbies = []
93+ for x in range (0 ,3 ):
94+ newhobbies = raw_input (" your new hobbies" )
95+ hobbies.append(str (newhobbies))
96+
97+ print hobbies
98+ ```
8199
82100
83- Using a list of lists in a function
84101
85102``` python
86103n = [[1 , 2 ], [4 , 5 , 6 , 7 ]]
@@ -94,8 +111,6 @@ def flatten(lists):
94111print flatten(n)
95112```
96113
97-
98-
99114``` python
100115board = []
101116for x in range (0 ,5 ):
@@ -104,6 +119,18 @@ for i in range(0,5):
104119 print board
105120```
106121
122+ or
123+
124+ ``` python
125+ board = []
126+ for n in range (5 ):
127+ board.append(' O' )
128+ def print_board (board ):
129+ for row in board:
130+ print board
131+ print_board(board)
132+ ```
133+
107134\[ 0, 0, 0, 0, 0\]
108135
109136\[ 0, 0, 0, 0, 0\]
@@ -121,25 +148,106 @@ for n in range(5):
121148print board
122149```
123150
151+ \[\[ 0, 0, 0, 0, 0\] , \[ 0, 0, 0, 0, 0\] , \[ 0, 0, 0, 0, 0\] , \[ 0, 0, 0, 0, 0\] , \[ 0, 0, 0, 0, 0\]\]
124152
153+ ``` python
154+ letters = [' a' , ' b' , ' c' , ' d' ]
155+ print " " .join(letters)
156+ print " ---" .join(letters)
157+ ```
125158
159+ a b c d
126160
161+ a---b---c---d---
127162
163+ ``` python
164+ board = []
165+ for x in range (0 ,5 ):
166+ board.append([" O" ] * 5 )
167+ def print_board (board ):
168+ for row in board:
169+ print " " .join(row)
128170
171+ print_board(board)
172+ ```
129173
174+ ---
130175
176+ ### While/else
131177
178+ ``` python
179+ import random
180+ print " Lucky Numbers! 3 numbers will be generated."
181+ print " If one of them is a '5', you lose!"
182+ count = 0
183+ while count < 3 :
184+ num = random.randint(1 , 6 )
185+ print num
186+ if num == 5 :
187+ print " Sorry, you lose!"
188+ break
189+ count += 1
190+ else :
191+ print " You win!"
192+ ```
132193
133194
134195
196+ 猜數字遊戲
135197
198+ ``` python
199+ from random import randint
200+ random_number = randint(1 , 10 )
201+ guesses_left = 3
202+ # Start your game!
203+ while guesses_left > 0 :
204+ guess = int (raw_input (" Your guess: " ))
205+ if guess == random_number:
206+ print " You win!"
207+ break
208+ guesses_left -= 1
209+ else :
210+ print random_number
211+ print " You lose."
212+ ```
213+
214+
215+
216+ 取代
217+
218+ ``` python
219+ phrase = " A bird in the hand..."
220+ s = " "
221+ for x in phrase:
222+ if x == ' A' or x == ' a' :
223+ s = s + ' X'
224+ else :
225+ s = s + x
226+ print s
227+ ```
228+
229+ X bird in the hand...
136230
137231
138232
233+ The` , ` character after our
139234
235+ ` print ` statement means that our next` print `
140236
237+ statement keeps printing on the same line.
141238
239+ ``` python
240+ phrase = " A bird in the hand..."
241+ # Add your for loop
242+ for char in phrase:
243+ if char == ' A' or char == ' a' :
244+ print ' X' ,
245+ else :
246+ print char,
247+ print
248+ ```
142249
250+ X bird in the hand...
143251
144252
145253
0 commit comments