51学通信论坛2017新版
标题:
1.5 While循环基础
[打印本页]
作者:
爱卫生
时间:
2017-12-12 10:58
标题:
1.5 While循环基础
本帖最后由 爱卫生 于 2017-12-12 14:19 编辑
先看一个简单的while循环。从0开始,数字每次+1,打印出来:
源码:
count = 0
while True:
print("count:",count)
count = count + 1 #和count +=1效果一样
复制代码
执行结果:
count: 842525
count: 842526
count: 842527
count: 842528
count: 842529
count: 842530
复制代码
发现计算机运算速度特别快,而且感觉是进入死循环了。所以不能这么写。(本例中,因为并没有定义count这个变量的上限,所以条件永远为真,就死循环了)
改进的代码如下,退出条件为count>1000则退出程序(注意if是缩进写在while里面的)
count = 0
while True:
print("count:",count)
count = count + 1 #和count +=1效果一样
if count > 1000:
break
复制代码
执行结果:
count: 997
count: 998
count: 999
count: 1000
复制代码
可以看到,count到了1000程序就结束了,这里的
break
就是退出程序。
接下来,把while循环应用到1.4里的猜年龄,如果不一定退出条件,就也是死循环。源码:
age_of_aiweisheng = 30
while True:
guess_age = int(input("guess age:"))
if guess_age == age_of_aiweisheng:
print("you are right.")
elif guess_age>age_of_aiweisheng:
print("try smaller")
else:
print("try bigger")
复制代码
执行结果:
guess age:36
try smaller
guess age:28
try bigger
guess age:30
you are right.
guess age:
复制代码
可以看到,最后用户猜对了年龄是30,也打印了you are right。但程序也没有结束退出。这个是不符合用户习惯的。
那接下来改进一下。也就是用户猜对了,程序退出,怎么改呢?很简单,在上面的代码中加一个break就可以了。
age_of_aiweisheng = 30
while True:
guess_age = int(input("guess age:"))
if guess_age == age_of_aiweisheng:
print("you are right.")
break
elif guess_age>age_of_aiweisheng:
print("try smaller")
else:
print("try bigger")
复制代码
执行结果:
guess age:29
try bigger
guess age:31
try smaller
guess age:30
you are right.
复制代码
再增加一个退出条件,就是用户只能猜3次。
接1.4猜年龄的例子,没有用while循环,用户猜一次(即使没猜对)就结束了,能不能让用户接着猜,直到猜出结果为止呢?可以。用while循环来处理。
源码:
本例中,让用户猜3次,猜对了结束,猜错了也只能猜3次。
while True:
if count == 3:
break
guess_age = int(input("guess age:"))
if guess_age == age_of_aiweisheng:
print("you are right.")
break
elif guess_age>age_of_aiweisheng:
print("try smaller")
else:
print("try bigger")
count = count +1
复制代码
执行结果:
guess age:39
try smaller
guess age:29
try bigger
guess age:31
try smaller
Process finished with exit code 0
复制代码
另外,如果第二次猜对了,那也会退出。
对上述代码可以进一步优化,不一定用while True,可以直接while count<3。也就是说只要count<3,条件就是为真,就一直循环执行下去。如下:
age_of_aiweisheng = 30
count = 0
while count<3:
guess_age = int(input("guess age:"))
if guess_age == age_of_aiweisheng:
print("you are right.")
break
elif guess_age>age_of_aiweisheng:
print("try smaller")
else:
print("try bigger")
count = count +1
else:
print("you have tried too many times.")
复制代码
但需要注意,这里在最后新增了一行提醒“您已经尝试太多次了!”。
这个print是在while的外面,也就是说当while不结束循环,那就不会执行最后一行的print。
继续优化。这次增加一个用户交互,用户猜3次都没对,我们问TA一下,你还玩吗?还玩打个Y继续猜,不玩就退出啦。
代码:
age_aiweisheng = 30
count = 0
while count < 3:
guess_age = int(input("guess age:"))
if guess_age == age_aiweisheng:
print("you guess right.")
break
elif guess_age > age_aiweisheng:
print("try smaller")
else:
print("try bigger")
count = count +1
if count ==3:
continue_guess = input("do you want keep guessing?")
if continue_guess != 'n':
count = 0
else:
print("you have tried too many times.")
复制代码
执行结果:
guess age:27
try bigger
guess age:26
try bigger
guess age:25
try bigger
do you want keep guessing?y
guess age:30
you guess right.
复制代码
欢迎光临 51学通信论坛2017新版 (http://bbs.51xuetongxin.com/)
Powered by Discuz! X3