一:流程控制if
语法一:
if 条件:
code1
code2
code3
...
age = 20
height = 170
weight = 60
sex = 'female'
is_beautiful = True if age> 12 and age<25 and weight == 60 and height >168 and sex == 'female' and is_beautiful:
print('这就是我的晓晖女神')
语法二
if 条件:
code1
code2
code3
...
else:
code1
code2
code3
...
age = 20
height = 170
weight = 60
sex = 'female'
is_beautiful = True if age> 12 and age<25 and weight == 60 and height >168 and sex == 'female' and is_beautiful:
print('这就是我的晓晖女神')
else:
print('走开走开!')
语法三:多分支
强调:if的多分支 = 但凡有一个条件成立.就会往下再判断其他条件了
if 条件1:
code1
code2
code3
...
elif :
code1
code2
code3
...
elif:
code1
code2
code3
...
else:
code1
code2
code3
...
小练习,判断成绩的等级
score=int(input('please input your score'))
if score>=90:
print('优秀')
elif score>=80:
print('良好')
elif score >=70:
print('普通')
else:
print('小伙子,你的成绩很差啊')
语法四:if 的嵌套
print('女神'.center(50,'*'))
name = 'xiaohui'
age = 18
height = 165
weight = 60
sex = 'female'
is_beautiful = True
if name == 'xiaohui' and age > 14 and age < 20 and height > 160 and sex == 'female':
print('心动的感觉')
if is_beautiful:
print('大声说,我喜欢你')
else:
print('逗你玩的呢')
else:
print('打扰了??????')
如果:今天是Monday,那么:上班
如果:今天是Tuesday,那么:上班
如果:今天是Wednesday,那么:上班
如果:今天是Thursday,那么:上班
如果:今天是Friday,那么:上班
如果:今天是Saturday,那么:出去浪
如果:今天是Sunday,那么:出去浪# today=input('>>: ')
if today == 'Monday':
print('上班')
elif today == 'Tuesday':
print('上班')
elif today == 'Wednesday':
print('上班')
elif today == 'Thursday':
print('上班')
elif today == 'Friday':
print('上班')
elif today == 'Saturday':
print('出去浪')
elif today == 'Sunday':
print('出去浪')
else:
print('''必须输入其中一种:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
''')
today=input('>>: ')
if today == 'Monday' or today == 'Tuesday' or today == 'Wednesday' or today == 'Thursday' or today == 'Friday':
print('上班')
elif today == 'Saturday' or today == 'Sunday':
print('出去浪')
else:
print('''必须输入其中一种:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
''')
二: 流程控制while
1 :什么是循环
循环就是一个重复的过程
2 为什么要有循环
我们想要让程序像人一样重复的去做某一件事情
3 怎么用循环
while 条件
code 1
code 2
code 3
...
username = 'andy'
password = ''
while Turn:
inp_name = input('please input your username')
inp_pwd = input('please input your password')
if inp_name == username and inp_pwd == password:
print('恭喜你,登陆成功')
else :
print('用户名或密码错误')
while+break 方法
在while循环中,break代表结束本层循环
代码实例:
username = 'andy'
password = ''
while Turn:
inp_name = input('please input your username')
inp_pwd = input('please input your password')
if inp_name == username and inp_pwd == password:
print('恭喜你,登陆成功')
break
else :
print('用户名或密码错误')
while+continue 方法
在while循环中,continue代表结束本次循环
代码实例
#删除掉1-100中所有包含4的数
#并且将剩下的数存到一个列表中
li=[]
for i in range(1,101):
if i[-1]==4:
continue
li.append(i) print(li)
while的嵌套循环:
username = 'andy'
password = ''
while Turn:
inp_name = input('please input your username')
inp_pwd = input('please input your password')
if inp_name == username and inp_pwd == password:
print('恭喜你,登陆成功')
print('下面是一个小程序')
while True:
inp= input('你叫什么名字?')
quest = inp + ',你好.我是人工智障'
agein_inp = input('输入q退出,输入其他开启智障模式')
if agein_inp=='q':
break
print(quest)
else :
print('用户名或密码错误')
while + else
else的代码会在while循环没有break打断的情况下最后运行
n=1 while n < 5: if n == 4: break print(n) n+=1 else: print('=====》')
上面的程序代码会被break打断,所以不会执行下面的else语句
三:流程控制for
for循环:可以不依赖索引而取值
#for 循环对象为列表 items = ['武大','武二','张三','李四','王五','赵六']
for item in items:
print(item) #for 循环对象为字典 dict = {'name':'andy','age':18,'height':180}
for dic in dict:
print(dic)
for vs while
for可以不依赖索引取值,是一种通用的循环取值方式
for循环次数是由被循环对象包含值的个数决定的,而while的循环次数是由条件决定的
nums = [1,2,3,4,5,6,2,5,6,3,6,9,9,5,4,3,5,3,6,3,]
for i in range(0,len(nums)):
print(nums[i])