流程控制语句-if 和循环语句-while
引入:如何让程序进行判断走哪一条岔路口?只需设置一些条件判断,满足那些条件,就走哪条路,这个过程叫做流程控制。----博客园 太白*金星
if 语句总共有四种形式
第一种:单个if
if True:
print(111)
#满足条件后要执行的代码
第二种:if…else
if 条件:
满足条件执行这段代码
else:
不满足条件执行这段代码
age=int(input('请输入你的年龄:'))
if age>=18:
print('adult')
else:
print('teenager')
第三种:if…elif…elif…else 多分支
if 条件:
满足条件执行代码
elif 条件:
上面的条件不满足就走这个
elif 条件:
上面的条件不满足就走这个
elif 条件:
上面的条件不满足就走这个
else:
上面所有的条件不满足就走这段
猜数字游戏:
print('让我们来玩一个猜数字的游戏吧!')
number=50
guess=int(input('请输入你猜的数字:'))
if guess<number:
print('too small')
elif guess>number:
print('too big')
else:
print('bingo')
第四种:嵌套 if
age=int(input('请输入你的年龄:'))
if age<18:
if age>=13:
print('teenager')
else:
print('kid')
else:print('adult')
注意:当有多个条件存在时,从上至下执行,若输入满足一个条件,执行这个,不会再往下执行。如下面这个例子:
成绩有ABCDE5个等级,与分数的对应关系如下:
A 90-100
B 80-89
C 70-79
D 60-69
E <60
要求用户输入0-100的数字后,你能正确打印他的对应成绩。
score=int(input('请输入你的分数:'))
if score>=100:
print('input error,the highest is 100!')
elif score>=90:
print('you got A,excellent!')
elif score>60:
print('you got D,passed')
elif score>70:
print('you got C, good!')
elif score>80:
print('you got B,great!')
else:print('you got E,failed!')
#输出
请输入你的分数:85
you got D,passed
这里把分数大于60这个代码移到了前面,当输入的值为85,大于60且大于80,输入满足一个条件,执行这个,不会再往下执行。
while 循环条件
语法:
while 条件:
#循环体
#当条件为真,执行循环体
#当条件为假,不执行循环体
死循环:
a=0
while True:
a+=1
print(a)
如何终止循环体呢?
1.改变条件,使其不成立
2.break 或者 continue语句
**思考:break和continue的区别是什么?**先来看一个例子
例1:输出1-100:
(小技巧:用flag进行标记,当需要终止循环时,只需对flag进行修改)
count=1
flag=True #flag进行标记
while flag:
while flag:
print(count)
count = count + 1
if count>100:
flag=False
那么,如果不用flag呢?
count=1
while count<=100:
print(count)
count+=1
用break改写以上程序
count=1
flag=True #flag进行标记
while flag:
while flag:
print(count)
count = count + 1
if count>100:
break
碰到 break 是立马跳出循环。而 continue 结束本次循环 继续下一次循环
count=1
while count<20:
print(count)
continue
count = count+1
程序会一直输出1,只是在循环体里面打转
例2 break:
count=0
while count<=100:
print('under loop',count)
if count==6: #如果count=6,跳出while循环。故只执行了6次循环
break
count += 1
print('----out of the while loop----')
#输出
under loop 1
under loop 2
under loop 3
under loop 4
under loop 5
under loop 6
----out of the while loop----
例3 continue:
count=0
while count<=100:
count += 1
if count>5 and count<95: #只要count大于5,且小于95.则不走下面的print语句,继续下一次循环。输出1-5之后,从95开始输出
continue
print('in loop', count)
print('----out of the while loop----')
while … else …
while 后面的else 作用是指,当while 循环正常执行完,中间没有被break 中止的话,就会执行else后面的语句
count = 0
while count <= 5 :
count += 1
print("in Loop",count)
else:
print("循环正常执行完啦")
print("-----out of while loop ------")
#输出
in Loop 1
in Loop 2
in Loop 3
in Loop 4
in Loop 5
in Loop 6
循环正常执行完啦
-----out of while loop ------
如果执行过程中被break的话,就不会执行else的语句
count = 0
while count <= 5 :
count += 1
if count == 3:break
print("Loop",count)
else:
print("循环正常执行完毕")
print("-----out of while loop ------")
#输出
Loop 1
Loop 2
-----out of while loop ------
———————————————————————————————
课后练习
1、使用while循环输出 1 2 3 4 5 6 8 9 10
number=0
while number<10:
number += 1
if number>6 and number <8: #可以用 number == 7 来简化
continue
print(number)
2、求1-100的所有数的和
a=0
sum=0
while a<=100:
sum=sum+a
a+=1
print(sum)
3、输出 1-100 内的所有奇数
number=0
while number<=100:
number=number+1
if number%2!=1:
continue
print(number)
4、输出 1-100 内的所有偶数
number=0
while number<=100:
number=number+1
if number%2==1: #判断对2取余数是否为1,是的话,不执行print。就继续接下来的循环。不为1,会执行print语句
continue
print(number)
5、求1-2+3-4+5 …+99-100的所有数的和
观察一下,所有偶数前面都是负号,奇数前面都是正号。
两种思路:
第一种,用-1**n控制正负号;
第二种,分别把所有的奇数和偶数都加起来,然后奇数的和-偶数的和
#第一种
index=1
a=0
sum=0
while a<100:
a+=1
index+=1
sum=sum+a*(-1)**index #这个指数思维不错!
print(sum)
#第二种
a=0
sum1=0
sum2=0
while a<100:
a+=1
if a%2==0: #如果为偶数全部加起来
sum2=sum2+a
if a%2==1: #如果为奇数也全部加起来
sum1=sum1+a
print(sum1-sum2) #奇数-偶数
6、用户登陆(三次机会重试)
password=123456
tries=0
while tries<3:
code=int(input('请输入你的密码:'))
if code!=password:
print ('密码错误')
else:
print('验证成功')
break
tries+=1
else:
print('您的账户已经被锁定,请20分钟以后再试')
加上验证用户名呢?
account='Lric'
password=123456
tries=0
while tries<3:
tries+=1
name=input('Account:')
code=int(input('Pssword:'))
if name==account and code==password:
print('log in successfully')
break
elif tries==3:
print('landing failed!')
else:
print('Account or Password error')
else:
print('Your account have been locked and try it 20min later!')
如果觉得这篇文章对你有小小的帮助的话,记得给文章点个“赞”哦,博主在此感谢!