练习代码
#佐料
zuoliao = ["辣椒","胡椒","盐"]
zuoliao = []
if zuoliao:
for add_zuoliuao in zuoliao:
if add_zuoliuao == "胡椒":
print("胡椒已用完!")
else:
print(str(add_zuoliuao) + "已添加!")
print("佐料已添加完毕,请用餐!")
else:
print("不好意思,佐料都用完啦!")
#判断顾客选的佐料是否在原始材料清单中
all_zuoliao = ["辣椒","胡椒","花椒","香菜","酱油","花生酱"]
want_zuoliao = ["辣椒","胡椒","番茄酱"]
for want_add_zuoliao in want_zuoliao:
if want_add_zuoliao in all_zuoliao:
print(str(want_add_zuoliao) + "已经添加!")
else:
print(str(want_add_zuoliao) + "我们没有!请重新选择!")
"""练习5-8 以特殊方式给管理员打招呼"""
yonghu = ["liding","admin","xiaobai","chenwusheng","python"]
yonghu = ["liding"]
print("用户登录后:")
if yonghu: #判断yonghu列表是否为空,若不为空for循环,为空打印没用户!
for dl_yonghu in yonghu:
if dl_yonghu == "admin":
print(str(dl_yonghu) + "你竟然是管理员?!")
else:
print(str(dl_yonghu) + "欢迎回来!")
else:
print("没用户!")
#检查用户名,判断重复
current_users = ["xiaobai","koulong","liding","Chenwusheng","AODI"]
new_users = ["ceshi","Chenwusheng","yaofengling","AODI","guoqian"]
if new_users:
for input_users in new_users:
if input_users.lower() or input_users in current_users: #lower将Input_users转换为小写 加入or ,满足一个条件即可;
print(str(input_users) + "用户名重复,请输入其他用户名!") # 意思为:new_users小写或原始字段都在current_users中,判断重复;
else:
print(str(input_users) + "用户名未被使用!")
else:
print("没有用户")
#序数
#写法1
xushu = range(1,10)
for xushu_put in xushu:
if xushu_put == 1:
print(str(xushu_put) + "st")
elif xushu_put == 2:
print(str(xushu_put) + "nd")
elif xushu_put == 3:
print(str(xushu_put) + "rd")
else:
print(str(xushu_put) + "th")
print("序数的另外一种写法:")
xushu2 = range(1,10)
for xushu2_put in xushu2:
if xushu2_put == 1:
value = "st"
elif xushu2_put == 2:
value = "nd"
elif xushu2_put == 3:
value = "rd"
else:
value = "th"
print(str(xushu2_put) + str(value))
输出结果
不好意思,佐料都用完啦!
辣椒已经添加!
胡椒已经添加!
番茄酱我们没有!请重新选择!
用户登录后:
liding欢迎回来!
ceshi用户名重复,请输入其他用户名!
Chenwusheng用户名重复,请输入其他用户名!
yaofengling用户名重复,请输入其他用户名!
AODI用户名重复,请输入其他用户名!
guoqian用户名重复,请输入其他用户名!
1st
2nd
3rd
4th
5th
6th
7th
8th
9th
序数的另外一种写法:
1st
2nd
3rd
4th
5th
6th
7th
8th
9th
Process finished with exit code 0
学习小结
今天已经学完第五章了,基本覆盖了以下知识点:
- 变量
- 字符串
- 数字
- 列表
- 元组
- if语句
- for…in…循环
学习时间在一周左右,每天都有坚持看书,看程序,知识点还需要在实际应用中实践加深记忆。想要实现同一个功能,程序往往会有多种不同的写法,只要逻辑清晰,剩下的应该就是经验了。