一,if 判断
---------示例1
# 单分支
age=25
salary=88
height=180
if age<28 and salary > 50 and height == 180:
print('hello,lets dinner together')
# 双分支
age=25
salary=88
height=180
if age<28 and salary > 50 and height == 180:
print('hello,lets dinner together')
else:
print('ugly man')
---------示例2
# 多分支
score=input("填入分数:")
score=int(score)
if score >= 90:
print("优秀")
if score > 100:
print("满分哦")
else:
print("距离满分差一乃乃")
elif score >=80:
print("良好")
else:
print("差")
二, while 循环
1) 基本用法
i=1
while i < 5:
print(i)
i+=1
1
2
3
4
2) 死循环
# 1)不会让cpu使用率飙升
while True:
input('name:')
# 2)会让cpu使用率飙升
while True:
1 + 1
3) 跳出/结束循环
1,条件为假
方式一:
#例1:
i=1
tag=True
while tag:
if i ==4:
tag=False
print(i)
i+=1
print('end')
1
2
3
4
end
# 例2:
i=1
tag=True
while tag:
print(i)
i+=1
if i ==4:
tag=False
print('end')
1
2
3
end
2,break
# (直接终止本层循环,根本没有下一次)
#例1:
i=1
while True:
if i == 4:
break
print(i)
i+=1
1
2
3
#例2:
i=1
while True:
print(i)
i+=1
if i == 4:
break
PS:
结果一样,第二种更好,只需要循环三次
第一种循环4次才结束
PS:
结束while循环的2种方式
tag=True
while tag:
while tag:
while tag:
tag=False
while True:
while True:
while True:
break
break
break
3,continue
#示例:
跳过4
i=1
while i <= 5:
if i == 4:
i += 1
continue
print(i)
i+=1
1
2
3
5
注意:
# continue同一级别的后面不要再写代码
# 循环体代码最后一步不要写continue
4,while+ else
i=1
while i <= 5:
print(i)
i+=1
else:
print('满足循环正常非break运行')
1
2
3
4
5
满足循环正常非break运行
============
i=1
while i <= 5:
if i == 4:
break
print(i)
i+=1
else:
print('满足循环正常非break运行')
1
2
3
5, 用户登录验证,三次密码错误登陆退出
# 方式一
count=0
while True:
name=input('name:')
password=input('password:')
if name == 'mz' and password == '123':
print('login succeed')
break
else:
print('faild login')
count+=1
if count == 3:
print('too many login,exiting')
break
name:zx
password:zx
faild login
name:zx
password:zx
faild login
name:zx
password:zx
faild login
too many login,exiting
name:mz
password:123
login succeed
======================
count=0
while True:
name=input('name:')
password=input('password:')
if name == 'mz' and password == '123':
print('login succeed')
while True:
print('输入操作编号')
choice=input('>>>')
if choice == '0':
break
elif choice == '1':
print('取款')
elif choice =='2':
print('转账')
break
else:
print('faild login')
count+=1
if count == 3:
print('too many login,exiting')
break
name:mz
password:123
login succeed
输入操作编号
>>>1
取款
输入操作编号
>>>2
转账
输入操作编号
>>>0
注意:
1)if name == 'mz' and password == '123': 要加引号
2)if choice == '0': 数字要加引号
3)if choice == '0':
break 连个break退出循环,对应两个while,位置不能放错
# 方式二
count=0
tag=True
name=input('name:')
passwd=input('passwd:')
while tag:
if name == 'mz' and passwd == '123':
print('login succeed')
while tag:
print('''
输入编码
0 退出
1 查询
2 转账
''')
choice=input('choice:')
if choice == '0':
tag = False
elif choice == '1':
print('查询')
elif choice == '2':
print('转账')
else:
print('输入错误,重新输入')
else:
print('faild to login')
count+=1
if count == 3:
print('too many times')
break
注意:
tag = True
使用变量作为条件,把条件改为假,只需要一次
tag = False
四,for循环
l=[11,22,33,44]
i=0
while i <len(l):
print(l[i])
i+=1
等同于
for x in l:
print(x)
11
22
33
44
======================
dic={"name":"mz","age":88}
for i in dic:
print(i)
name
age
dic={"name":"mz","age":88}
for i in dic:
print(i,dic[i])
name mz
age 88
======================
info=[["name","mz"],["age",22]]
for i in info:
print(i)
info=[["name","mz"],["age",22]]
for x,y in info:
print(x,y)
['name', 'mz']
['age', 22]
name mz
age 22
五,range范围
1)使用
i=0
while True:
if i < 3:
print('hello')
i+=1
等同于
l=[11,22,33]
for x in l:
print('hello')
等同于
for x in range(3):
print('hello')
hello
hello
hello
PS:
python3直接范围取值(老母鸡)
python2是下蛋,占内存
2)for + break
# for + break
for x in range(5):
if x == 3:
break
print(x)
0
1
2
# for + continue
for i in range(5):
if i ==3:
continue
print(i)
0
1
2
4
#for + else
for i in range(5):
if i ==3:
continue
print(i)
else:
print('endendend')
0
1
2
4
endendend