while 循環 能夠提升代碼的效率,減小代碼的冗餘spa
while 條件表達式:
code1
code2
若是條件表達式成立,返回Ture,就執行其中的代碼塊code
例:打印1 ~100排序
#(1)初始化變量i文檔
i = 1
#(2)寫循環判斷條件
while i<= 100:
print(i)
#(3)自增自減的條件
i+=1
#代碼解析
首先初始化i=1
而後判斷1<=100 知足返回Ture 執行代碼塊
而後print(1)
i+=1 i = i + 1 i =>2
回到條件表達式當中進行判斷 也就是回答20行
2<=100 知足 返回Ture 執行代碼塊
i+=1 i= i+1 2+i =>3
回到條件表達式當中進行判斷 ,也就是回到20行
3<=100 知足 返回Ture 執行代碼塊
而後print(3)
i+=1 i = i + 1 3+1 =>4
何時條件跳出?
當i=101的時候
101<=100 不知足 返回False 不執行代碼塊
循環終止。。。input
例:class
#(1)第一種方法
#1~100的累加和
i = 1
total = 0
while i <= 100:
#寫上邏輯
#print(i)
total += 1
i += 1
print(total)
代碼解析:
total += i => total = total + i => 0 + 1 => 1
i+=1 => i = i+1 => 1+1 => 2
2 <= 100 滿 足 返回真True
total += i => totoal = total + i => 0 + 1 + 2 => 3
i+=1 => i = i+1 => 2+1 => 3
3 <= 100 知足 返回真True
total += i => totoal = total + i => 0 + 1 + 2 + 3 => 6
i+=1 => i = i+1 => 3+1 => 4
4 <= 100 知足 返回真True
total += i => totoal = total + i => 0 + 1 + 2 + 3 + 4 + 5 + ...+ 100 => 5050
dai
當i = 101 的時候
101 <= 100 不知足 循環終止...
# (2)死循環寫法
#whilie Ture:
#print(1)
i = 1
flag = True
total = 0
while flag:
total += i
i +=1
# 添加一個可以跳出來的條件
if i == 101:
flag = False
print(total)
#打印一行10個小星星
#help 能夠查看幫助文檔
help(print)
i = 1
while i <=100:
#end = '' 默認不換行
print("*",end="")
i+=1
#用一個變量輸出10個小星星(配置while)效率
i = 0
while i < 10:
print("★",end="")
i+=1變量
(十個小星星塞在一個變量中,最後達成變量)
例:
#法一:
a = 10
res = "★" * a
print(res)
print("===============")
#法二:
i = 0
strvar = ''
while i<10:
strvar += "★"
i+=1
print(strvar)
print("===============")
#法三:
i = 0
while i < 10:
print("★",end="")
i+=1
print("\n",end="===============\n")
#法四:
print("★★★★★★★★★★")配置
例:循環
#法一:
i = 0
while i < 100:
#打印星星
print("☆",end="")
#打印換行
if i%10 == 9:
print()
i+=1
#法二:
print("===============")
i = 0
while i <= 10:
j = 0
while j <= 10:
print("☆",end="")
j += 1
print("\n",end="")
i += 1
#法三:
#輸入數量幾就輸出幾個幾行小星星,且奇數爲★,偶數爲☆
i = 0
n = float(input("請輸入星星個數:"))
while i < n :
if n % 2 == 0:
j = 1
while j <= n:
print("☆", end="")
j += 1
print("\n", end="")
i+=1
else:
j = 1
while j <= n:
print("★", end="")
j += 1
print("\n", end="")
i += 1
#法四:
j = 0
n = int(input("請輸入星星個數:"))
for j in range(0,n):
i = 0
strvar = ''
while i<n:
if n % 2 == 0:
strvar += "★"
else:
strvar += "☆"
i+=1
print(strvar)
j+=1
例:
#法一
i = 1
while i <= 100:
#控制打印 隔列打印
if i % 2==0:
print("★", end="")
else:
print("☆",end="")
#控制換行
if i % 10 == 0:
print()
i +=1
print("==================")
#法二:
i = 0
strvar = ''
j = 0
while j < 10:
while i < 10:
if i%2 == 0:
strvar +="★"
else:
strvar += "☆"
i+=1
print(strvar)
j += 1
print("==================")
#法三:
j = 0
#對行的星星進行排序
for j in range(0,10):
i = 0
strvar = ''
#對列的星星進行排列
while i < 10:
if i%2 == 0:
strvar +="★"
else:
strvar += "☆"
i+=1
print(strvar)
j+=1
#法四
count = 1
while count <= 100:
if count & 1:
print("★", end="")
else:
print("☆",end="")
if count % 10 == 0:
print("\n",end="")
count +=1
print("=======最後一種========")
i = 0
while i < 10:
j = 0
while j < 10:
#控制打印星星的代碼
if j % 2 == 0:
print("★",end="")
else:
print("☆",end="")
j+=1
print()
i += 1
例:
#法一
i = 0
while i < 10:
j = 0
if i%2 == 0:
while j <= 10:
print("★",end="")
j += 1
print("\n",end="")
else:
while j <= 10:
print("☆",end="")
j += 1
print("\n",end="")
i += 1
print("=========================")
解析:
n // 2
0 // 2 0
1 // 2 0
2 // 2 1
3 // 2 1
0 // 4 0
1 // 4 0
2 // 4 0
3 // 4 0
4 // 4 1
..
..
4個1 4個2 4個3
任意數和n進行地板除 產生n個相同的數
任意數 // 10 產生十個相同的數
任意數 // 25 產生25個相同的數
#法二
i = 0
while i< 100:
#打印星星的部分
if i // 10 %2 == 0:
print("★",end="")
else:
print("☆",end="")
#執行部分
if i % 10 == 9:
print()
i+=1
解析:
i = 0 1 2 3 4 5 6 7 8 9
i // 10 0
i = 10 11 12 13 14 15 16 17 18 19
i //10 1
...........
i // 10 2
i // 10 獲得最後的數範圍是0~9
0~9 % 2 0 或者1 正好作黑白星
例:
j = 0
m = int(input("請輸入小星星的行數:"))
n = int(input("請輸入小星星的列數:"))
#對行的星星進行排序
for j in range(0,m):
i = 0
strvar = ''
#對列的星星進行排列
while i < n:
if i%2 == 0:
strvar +="★"
else:
strvar += "☆" i+=1 print(strvar) j+=1