小白自学笔记之Python入门-第六章 循环-2
6.2 while循环
while 循环的语法如下:
while 条件:
#循环体
else:
#条件为 False 时执行
运行下面的例子,分析结果,提高程序阅读能力。
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)
程序运行结果:
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)
程序运行结果:
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
下面我们来实现超市购物系统。
抱歉,知识储备依然不够,接着看下面的break & continue吧!
6.3 break & continue
在某些情况下(比如润杰学生公寓到三教路上的物美超市征集10名吃辣志愿者,一个一个咨询走过来的顾客,最多问100个人,先到先得,典型的吃货想的例子,估计问了不到20名就能征集到10名勇士了),我们希望在循环结束前就退出循环(不需要问到100个人),因此需要使用如下break和continue用法:
1. 使用 continue 语句,可以跳过执行本次循环体中剩余的代码,转而执行下一次的循环。
2. 只用 break 语句,可以完全终止当前循环。
break和continue区别如下:

一个例子解释清楚break和continue。试一下下面的代码,分析结果:
# 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)
程序运行结果:
break例子
你好 0
你好 1
你好 2
你好 3
你好 4
你好 5
************************
coninue例子
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
遇到5了
Hello 6
Hello 7
编程练习:
1. 判断一个整数是否为素数(这个上课应该会讲的)。
终于可以实现我们的超市购物系统了,超市购物系统源代码如下:
print(" ***超市购物系统*** ")
print("欢迎光临,祝您购物愉快!")
flag = True
message = ''
total=0
count=0
goods=[] #这是个啥?
while flag :
price=float(input("请输入商品价格,输入 0 结束:"))
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("应付金额为 :",format(total,"0.2f"))
print("顾客付款金额 :",format(paymoney,"0.2f"))
print("找零 :",format(change,"0.2f"))
print("欢迎再来!Have a good day!")
先解释一下疑问,其实学习完组合数据章节中列表list大家就理解了,就是一种数据类型而已,可以直达第七章组合数据中7.1列表一节自学一下……或者试着往下看,万一看懂了呢。
goods=[] #这是个啥?
goods=[ ]是列表表示一组数据,就像我们数学里学过的数列:a1, a2, a3……an 因为不知道购买多少个商品,所以不能定义固定个数的变量,而且即便知道有20种商品,也不应该通过定义20个变量的方式,而应该使用列表的方式,使用列表就可以通过循环访问数据,简化程序编写。
goods.append(price) #这又是个啥?
将price的值追加到列表中,就是记录下当前商品的价格
goods[i] #还有这个?
表示列表goods中第i个数据的值,类似 ai ,可以通过循环处理,简单多了。
程序运行结果:
***超市购物系统***
欢迎光临,祝您购物愉快!
请输入商品价格,输入 0 结束:5
请输入商品价格,输入 0 结束:66
请输入商品价格,输入 0 结束:44
请输入商品价格,输入 0 结束:23
请输入商品价格,输入 0 结束:11
请输入商品价格,输入 0 结束:68
请输入商品价格,输入 0 结束:0
商品金额为 : 217.0
请您付款:300
***购物清单***
第 1 种商品价格 : 5.0
第 2 种商品价格 : 66.0
第 3 种商品价格 : 44.0
第 4 种商品价格 : 23.0
第 5 种商品价格 : 11.0
第 6 种商品价格 : 68.0
应付金额为 : 217.00
顾客付款金额 : 300.00
找零 : 83.00
欢迎再来!Have a good day!
编程练习:
1. 求最大公约数和最小公倍数,这个可以自己完成了!
2. 猜数字游戏:系统随机生成一个1~100的数字,用户共有5次机会猜,如果用户猜测数字大于系统给出的数字,打印"too big",如果用户猜测数字小于系统给出的数字,打印"too small",如果用户猜测的数字等于系统给出的数字,打印"恭喜中奖",并退出循环。
import random
num = random.randint(1,100)
print(num) #这个是否有作弊的嫌疑呢?
i = 1
while i <= 5:
ans = int(input("请猜数:"))
if ans > num:
print("too big,还剩%d次机会"%(5-i))
elif ans < num:
print("too small,还剩%d次机会"%(5-i))
else:
print("恭喜中奖!!!")
break
i += 1
35
请猜数:50
too big,还剩4次机会
请猜数:25
too small,还剩3次机会
请猜数:40
too big,还剩2次机会
请猜数:30
too small,还剩1次机会
请猜数:35
恭喜中奖!!!