小白自學(xué)筆記之Python入門-第六章 循環(huán)-2
6.2 while循環(huán)
while 循環(huán)的語法如下:
while 條件:
??? #循環(huán)體
else:
? ? #條件為 False 時執(zhí)行
?
運(yùn)行下面的例子,分析結(jié)果,提高程序閱讀能力。
message = 'This is EchoGame,you enter string ,I will repeat it back to you .'
message += "\nEnter 'quit' to end the program. Enter your message here :"
user_input = ''
while user_input != 'quit':
??? user_input = input(message)
print("Echo : ",user_input)
?
程序運(yùn)行結(jié)果:
This is EchoGame,you enter string ,I will repeat it back to you .
Enter 'quit' to end the program. Enter your message here :CUP
Echo :? CUP
This is EchoGame,you enter string ,I will repeat it back to you .
Enter 'quit' to end the program. Enter your message here :cup
Echo :? cup
This is EchoGame,you enter string ,I will repeat it back to you .
Enter 'quit' to end the program. Enter your message here :quit
Echo :? quit
?
還可以這樣做:
flag = True
message = ''
while flag :
??? message = input("Input something please,enter 'quit' to end the program:")
??? if message == 'quit':
??????? flag = False
??? else:
??????? print('Echo :'+message)
?
程序運(yùn)行結(jié)果:
Input something please,enter 'quit' to end the program:echo
Echo :echo
Input something please,enter 'quit' to end the program:love
Echo :love
Input something please,enter 'quit' to end the program:quit
?
下面我們來實(shí)現(xiàn)超市購物系統(tǒng)。
抱歉,知識儲備依然不夠,接著看下面的break & continue吧!
6.3 break & continue
在某些情況下(比如潤杰學(xué)生公寓到三教路上的物美超市征集10名吃辣志愿者,一個一個咨詢走過來的顧客,最多問100個人,先到先得,典型的吃貨想的例子,估計(jì)問了不到20名就能征集到10名勇士了),我們希望在循環(huán)結(jié)束前就退出循環(huán)(不需要問到100個人),因此需要使用如下break和continue用法:
1. 使用 continue 語句,可以跳過執(zhí)行本次循環(huán)體中剩余的代碼,轉(zhuǎn)而執(zhí)行下一次的循環(huán)。
2. 只用 break 語句,可以完全終止當(dāng)前循環(huán)。
break和continue區(qū)別如下:

一個例子解釋清楚break和continue。試一下下面的代碼,分析結(jié)果:
# break
print("break例子")
for i in range(8):
??? print("你好", i)
??? if i == 5:
??????? break
# coninue
print("************************\nconinue例子")
for i in range(8):
??? if i == 5:
??????? print("遇到 5 了")
??????? continue
??? print("Hello", i)
?
程序運(yùn)行結(jié)果:
break例子
你好 0
你好 1
你好 2
你好 3
你好 4
你好 5
************************
coninue例子
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
遇到5了
Hello 6
Hello 7
編程練習(xí):
1. 判斷一個整數(shù)是否為素?cái)?shù)(這個上課應(yīng)該會講的)。
?
終于可以實(shí)現(xiàn)我們的超市購物系統(tǒng)了,超市購物系統(tǒng)源代碼如下:
print("??????? ***超市購物系統(tǒng)***??????? ")
print("歡迎光臨,祝您購物愉快!")
flag = True
message = ''
total=0
count=0
goods=[]????????????????????? ?????????????????#這是個啥?
while flag :
??? price=float(input("請輸入商品價格,輸入 0 結(jié)束:"))
??? if price==0:
??????? flag=False
??????? break
??? total = total + price
??? goods.append(price)?????????? ???????????????#這又是個啥?
??? count = count + 1
?
print("商品金額為?????? :",total)
paymoney=float(input("請您付款:"))
change=paymoney-total
?
print("??????? ***購物清單***??????? ")
for i in range(count):
??? print("第",i+1,"種商品價格?? :",goods[i])? ?????#還有這個?
?
print("應(yīng)付金額為?????? :",format(total,"0.2f"))
print("顧客付款金額???? :",format(paymoney,"0.2f"))
print("找零??????????? :",format(change,"0.2f"))
print("歡迎再來!Have a good day!")
?
先解釋一下疑問,其實(shí)學(xué)習(xí)完組合數(shù)據(jù)章節(jié)中列表list大家就理解了,就是一種數(shù)據(jù)類型而已,可以直達(dá)第七章組合數(shù)據(jù)中7.1列表一節(jié)自學(xué)一下……或者試著往下看,萬一看懂了呢。
??? goods=[]? #這是個啥?
goods=[ ]是列表表示一組數(shù)據(jù),就像我們數(shù)學(xué)里學(xué)過的數(shù)列:a1, a2, a3……an 因?yàn)椴恢蕾徺I多少個商品,所以不能定義固定個數(shù)的變量,而且即便知道有20種商品,也不應(yīng)該通過定義20個變量的方式,而應(yīng)該使用列表的方式,使用列表就可以通過循環(huán)訪問數(shù)據(jù),簡化程序編寫。
??? goods.append(price)? ?#這又是個啥?
將price的值追加到列表中,就是記錄下當(dāng)前商品的價格
??? goods[i]? #還有這個?
表示列表goods中第i個數(shù)據(jù)的值,類似 ai ,可以通過循環(huán)處理,簡單多了。
?
程序運(yùn)行結(jié)果:
??????? ***超市購物系統(tǒng)***???????
歡迎光臨,祝您購物愉快!
請輸入商品價格,輸入 0 結(jié)束:5
請輸入商品價格,輸入 0 結(jié)束:66
請輸入商品價格,輸入 0 結(jié)束:44
請輸入商品價格,輸入 0 結(jié)束:23
請輸入商品價格,輸入 0 結(jié)束:11
請輸入商品價格,輸入 0 結(jié)束:68
請輸入商品價格,輸入 0 結(jié)束:0
商品金額為?????? : 217.0
請您付款:300
??????? ***購物清單***???????
第 1 種商品價格?? : 5.0
第 2 種商品價格?? : 66.0
第 3 種商品價格?? : 44.0
第 4 種商品價格?? : 23.0
第 5 種商品價格?? : 11.0
第 6 種商品價格?? : 68.0
應(yīng)付金額為?????? : 217.00
顧客付款金額???? : 300.00
找零??????????? : 83.00
歡迎再來!Have a good day!
?
編程練習(xí):
1. 求最大公約數(shù)和最小公倍數(shù),這個可以自己完成了!
2. 猜數(shù)字游戲:系統(tǒng)隨機(jī)生成一個1~100的數(shù)字,用戶共有5次機(jī)會猜,如果用戶猜測數(shù)字大于系統(tǒng)給出的數(shù)字,打印"too big",如果用戶猜測數(shù)字小于系統(tǒng)給出的數(shù)字,打印"too small",如果用戶猜測的數(shù)字等于系統(tǒng)給出的數(shù)字,打印"恭喜中獎",并退出循環(huán)。
import random
?
num = random.randint(1,100)
print(num)? #這個是否有作弊的嫌疑呢?
?
i = 1
while i <= 5:
??? ans = int(input("請猜數(shù):"))
??? if ans > num:
??????? print("too big,還剩%d次機(jī)會"%(5-i))
??? elif ans < num:
??????? print("too small,還剩%d次機(jī)會"%(5-i))
??? else:
??????? print("恭喜中獎?。?!")
??????? break
??? i += 1
?
35
請猜數(shù):50
too big,還剩4次機(jī)會
請猜數(shù):25
too small,還剩3次機(jī)會
請猜數(shù):40
too big,還剩2次機(jī)會
請猜數(shù):30
too small,還剩1次機(jī)會
請猜數(shù):35
恭喜中獎?。?!