9.14学习内容
1.1 if判断方法
i = 0
if i < 1:
print('successful')
1.2 if ,else
age=18
sex='male'
wuzhong='human'
is_beautiful=True
if age > 16 and age < 22 and sex == 'female' and \
wuzhong == 'human' and is_beautiful:
print('开始表白...')
else:
print('阿姨好,我逗你玩呢...')
1.3 if ,else, elif
score = input('your score>>: ')
score=int(score)
score >= 90:
print('优秀')
elif score >= 80:
print('良好')
score >= 70:
print('普通')
else:
print('很差')
二 while 循环
1. 什么是循环
循环指的是一个重复做某件事的过程
2.
为何要有循环
为了让计算机能够像人一样重复做某件事
2.1 while循环的语法:while循环又称为条件循环,循环的次数取决于条件
print('start....')
while True:
name=input('please your name>>: ')
pwd=input('please your password>>: ')
if name == 'egon' and pwd == '123':
print('login successful')
else:
print('user or password err')
print('end...')
2.2 while ,break强行终止本层循环
count=1
while True:
if count > 5:
break
print(count)
count+=1
2.3 while+continue:continue代表结束本次循环,直接进入下一次
count=1
while count < 6:
if count == 4:
count+=1
continue # 只能在cotinue同一级别之前加代码
print(count)
count+=1
2.4 while + else:
while+else
count=1
while count < 6:
if count == 4:
break
print(count)
count+=1
else:
print('会在while循环没有被break终止的情况下执行')
2.5 while循环的嵌套
name_of_db='egon'
pwd_of_db='123'
print('start....')
count=0
while count <= 2: #count=3
name=input('please your name>>: ')
pwd=input('please your password>>: ')
if name == name_of_db and pwd == pwd_of_db:
print('login successful')
while True:
print("""
1 浏览商品
2 添加购物车
3 支付
4 退出
""")
choice=input('请输入你的操作: ') #choice='1'
if choice == '1':
print('开始浏览商品....')
elif choice == '2':
print('正在添加购物车....')
elif choice == '3':
print('正在支付....')
elif choice == '4':
break
break
else:
print('user or password err')
count+=1
else:
print('输错的次数过多')
print('end...')
三 for循环主要用于循环取值
list 目录取值
student=['hong','lan','qin,''zi','hei']
for item in student:
print(item)
str 字符串取值
for item in 'hello':
# print(item)
dict 字典取值
dic={'x':444,'y':333,'z':555}
for k in dic:
print(k,dic[k])
range范围 取值
for i in range(1,10,3): (代表范围1-10,每次出值跳3步)
print(i)
len方法可以获取值的长度
student=['hong','lan','qin,''zi','hei']
for i in range(len(student)):#len获取列表的长度,range根据这个范围把key值给 i
print(i,student[i]) #通过key值把value取出