一、流程控制---if
1.if条件判断
age=18
hight=1.70
sex="female"
is_beautiful=True
if sex=="female" and age>16and age<20 and is_beautiful \
and hight>1.60 and hight<1.75:
print("表白。。。。")
2.if+else
hight=1.70
sex="female"
is_beautiful=True
if sex=="female" and age>16and age<20 and is_beautiful \
and hight>1.60 and hight<1.75:
print("表白。。。。")
if is_beautiful:
print("在一起。。。")
else:
print("什么爱情不爱情""和我有半毛钱关系")
3.if的嵌套:
#语法三:if 的嵌套
age=18
hight=1.70
sex="female"
is_beautiful=True
if sex=="female" and age>16and age<20 and is_beautiful \
and hight>1.60 and hight<1.75:
print("表白。。。。")
if is_beautiful:
print("在一起。。。")
else:
print("什么爱情不爱情""和我有半毛钱关系")
else:
print("阿姨好。。。")
print(" code ...")
4.if-elif的运用:
如果 成绩>=90,那么:优秀
如果 成绩>=80且<90,那么:良好
如果 成绩>=70且<80,那么:普通
其他情况:很差
"""score=input("输入成绩:")
score=int(scar)
if scar>=90:
print("优秀")
elif score>=80:
print("良好")
elif score>=70:
print("普通")
else:
print("极差")"""
二、流程控制--while
1.while循环又称为条件循环
while条件循环
"""
while True:
name=input("please input yourname:")
password=input("please input your passwor;")
if name=="adb" and password=="123":
print("login successful")
else:
print("username or password error")
2.while+条件结束
tag=True
while tag:
name=input("please input yourname:")
password=input("please input your passwor;")
if name=="adb" and password=="123":
print("login successful")
tag=False
else:
print("username or password error")
3.while+break:退出本层循环
while True:
name = input("please input yourname:")
password = input("please input your passwor;")
if name == "adb" and password == "123":
print("login successful")
break
4.while+continue:终止本次循环,进行下次循环,跳过了本次循环之后的代码
count=0
while count<6:
if count==4:
count+=1
continue
print(count)
count+=1
5.while+else:当while循环正常执行完,中间没有break人为中止的话,执行else
# ---while-else----
count = 1
while count < 6:
if count == 4:
count += 1
continue
print(count)
count += 1
else:
print("while-else最后else的代码")
三、流程控制--for
for循环又称为迭代循环
for可以不依赖于索引取指,是一种通用的循环取指方式
循环次数是由被循环对象包含值的个数决定的,而while的循环次数是由条件决定的
for+break
for+continute
for+else
和while一样的用法
1.0 range() 》》》通常用于取值不(依赖索引)
range(1,5)
>>>1 2 3 4#g顾头不顾尾,
所以只能取到
range(5)
>>>1 2 3 4
range(1,5,2) #步偿 ,每隔两个取值#i = i + 2
>>>1 3
2.0嵌套循环:
for i in range(1,10):
for j in range(1,i+1):
print("%s*%s=%s" %(i,j,i*j,end="")
print()#换行
九九乘法表
本章练习:
.练习,要求如下:
1.1用户登录:
name=input("please your name:")
password=input("please your password:")
if name =="egon" and password=="123456":
print("login successful....")
break
else:
print("name or password error....")
1.2管理员登录系统:
egon --> 超级管理员
tom --> 普通管理员
jack,rain --> 业务主管
其他 --> 普通用户
ame=input("请输入名字:")
if name=="egon":
print("超级管理员")
elif name=="tom":
print("普管理员")
elif name=="jick,rain":
print("业务主管")
else:
print("普通用户")
1.3
1 如果:今天是Monday,那么:上班 2 # 如果:今天是Tuesday,那么:上班 3 # 如果:今天是Wednesday,那么:上班 4 # 如果:今天是Thursday,那么:上班 5 # 如果:今天是Friday,那么:上班 6 # 如果:今天是Saturday,那么:出去浪 7 # 如果:今天是Sunday,那么:出去浪 8 9 """ 10 """ 11 today_is=input("今天是礼拜几:") 12 if today_is=="monday" or today_is=="tuestday" or today_is=="wedneesday"or \ 13 today_is=="thursday"or today_is=="friday": 14 print("working...") 15 elif today_is=="sunday" or today_is=="saturday": 16 print("playing...") 17 else: 18 print("必须输入:" 19 "monday" 20 "Tuesday", 21 "Wednesday", 22 "Thursday", 23 "Friday", 24 "Saturday", 25 "Sunday" )
2.1.while循环练习题
1 循环验证用户输入的用户名与密码
2 认证通过后,运行用户重复执行命令 3 当用户输入命令为时,则退出整个程序
name = "egon"
password = "123456"
count = 0
while True:
inp_name = input("please your name:")
inp_password = input("please your password:")
if name == inp_name and password == inp_password:
print("login successful....")
print("""
1 取钱
2 存款
3 退出
""")
while True:
choice = input("please input your choice")
if choice == "1":
print("取钱")
elif choice == "2":
print("存钱")
elif choice == "3":
break
else:
print("你的输入有误,请重新输入")
else:
print("name or password error....")
count += 1
if count == 3:
break
2.1 迭代式循环:for for in range():作业九九乘法表
for i in range(1,10):
for j in range(1,i+1):
print("%s*%s=%s" %(i,j,i*j,end="")
print()
1*1=1
2*1=22*2=4
3*1=33*2=63*3=9
4*1=44*2=84*3=124*4=16
5*1=55*2=105*3=155*4=205*5=25
6*1=66*2=126*3=186*4=246*5=306*6=36
7*1=77*2=147*3=217*4=287*5=357*6=427*7=49
8*1=88*2=168*3=248*4=328*5=408*6=488*7=568*8=64
9*1=99*2=189*3=279*4=369*5=459*6=549*7=639*8=729*9=81
2.2 制作金字塔
# 先打印最外层
max_level = input('输入层数》》》:').strip()
max_level = int(max_level)
for current_level in range(1,max_level+1):
# print(current_level)
# 打印空格
for i in range(max_level-current_level):
# print(i)
print(' ',end='')
# 打印星星数
for j in range(2*current_level-1):
# print(j)
print('*', end='')
print()
感谢您的阅览,有不足之处还请之处,共同学习! 内容持续更新中。。。