While Loops
...
當(dāng)用戶重復(fù)問一個問題時(shí)镀虐,Python如何檢測箱蟆?
以汽車啟動還是停止這個具體問題來說,機(jī)器需要知道汽車的狀態(tài)是開動還是停止刮便,SO需要一個汽車的狀態(tài)這個變量空猜。
狀態(tài)是啟動還是暫停,這里要用的運(yùn)算符是:布爾邏輯運(yùn)算True FALSE
started = False
For loops>
for 循環(huán)語句
集合collection的形式: string-list- range object
[1, 2, 3,....] 輸出列表list
range() 輸出容器恨旱,一個范圍范圍容器
range(10) 輸出0,1,2辈毯,。搜贤。谆沃。9
range(5,10) 輸出5,6,7,8,9
range(5,10,2) 這里的2為step,所以 輸出為5,7,9
在某一個循環(huán)
中仪芒,一定要跟對應(yīng)的循環(huán)函數(shù)對齊管毙,否則會產(chǎn)生錯誤:
nested LOOP 嵌套循環(huán)
用來生成多坐標(biāo)數(shù)組
執(zhí)行循環(huán)的順序是
先從上至下執(zhí)行外部循環(huán),一旦進(jìn)入內(nèi)部循環(huán)桌硫,先把內(nèi)部循環(huán)執(zhí)行完,
再回到外部循環(huán)啃炸。
numbers = [5, 2, 5, 2, 2]
for x_count in numbers:
output = ''
for count in range(x_count): #內(nèi)循環(huán)
output += 'x'
print(output)
D:\Aaron\Python\PyCharmProject\Scripts\python.exe D:/Aaron/Python/HelloWorld/app.py
xxxxx
xx
xxxxx
xx
xx
Process finished with exit code 0
作業(yè):找出序列中铆隘,最大的數(shù)字
numbers = [3, 6, 8, 5, 9, 10]
max = numbers[0]
for number in numbers:
if number > max:
max = number
print(max)
D:\Aaron\Python\PyCharmProject\Scripts\python.exe D:/Aaron/Python/HelloWorld/app.py
10
Process finished with exit code 0