python 第七天作业代码

# # 1:编写for循环,利用索引遍历出每一个字符(选做题)
msg = 'hello egon 666'
for i in range(len(msg)):
print(msg[i])
# # 2:编写while循环,利用索引遍历出每一个字符
msg = 'hello egon 666'
count = 0
while count < len(msg):
print(msg[count])
count += 1
# # 3:
msg = 'hello alex'
# 中的alex替换成SB
print(msg.replace('alex', 'SB'))
# # 4:
msg = '/etc/a.txt|365|get'
# 将该字符的文件名,文件大小,操作方法切割出来
print(msg.split('|'))
# # 5.
# # 编写while循环,要求用户输入命令,如果命令为空,则继续输入
while True:
cmd = input('>>>:').strip()
if len(cmd) == 0:continue
break
# # 6.
# # 编写while循环,让用户输入用户名和密码,如果用户为空或者数字,则重新输入
while True:
user = input('name:').strip()
pwd = input('pwd:').strip()
if len(user) == 0 or user.isdigit():continue
break
# 7.
# 编写while循环,让用户输入内容,判断输入的内容以alex开头的,则将该字符串加上_SB结尾
while True:
user = input('内容:')
if user.startswith('alex'):
print(user+'_SB')
break
# # 8.
# # 1.
# # 两层while循环,外层的while循环,让用户输入用户名、密码、工作了几个月、每月的工资(整数),用户名或密码为空,或者工作的月数不为整数,或者
# # 月工资不为整数,则重新输入
# while True:
# user = input('user:')
# if len(user) == 0:continue
# while True:
# pwd = input('pwd:')
# if len(pwd) == 0: continue
# break
# while True:
# month = input('month:')
# if not month.isdigit(): continue
# break
# while True:
# salary = input('salary:')
# if not salary.isdigit(): continue
# break
# break
# 2.
# 认证成功,进入下一层while循环,打印命令提示,有查询总工资,查询用户身份(如果用户名为alex则打印super
# user,如果用户名为yuanhao或者wupeiqi
# 则打印normal
# user,其余情况均打印unkown
# user),退出功能
# while True:
# user = input('>>>:').strip()
# if user == 'alex': print('super')
# elif user == 'yuanhao' or user == 'wupeiqi':print('normal')
# else: print('unkown')
# 3.
# 要求用户输入退出,则退出所有循环(使用tag的方式)
# 运行效果如下:
# user: egon
# password: 123
# work_mons: 12
# salary: 10000
#
# 1
# 查询总工资
# 2
# 查询用户身份
# 3
# 退出登录
#
# >>: 1
# 总工资是: 120000.0
#
# 1
# 查询总工资
# 2
# 查询用户身份
# 3
# 退出登录
#
# >>: 2
# unkown
# user
#
# 1
# 查询总工资
# 2
# 查询用户身份
# 3
# 退出登录
#
# >>: 3
tag = True
while tag:
user = input('user:')
if len(user) == 0:continue
pwd = input('pwd:')
if len(pwd) == 0: continue
month = input('month:')
if not month.isdigit(): continue
salary = input('salary:')
if not salary.isdigit(): continue
print('user:{name}\npassword:{pwd}\nwork_mons:{month}\nsalary:{salary}'.format(name=user, pwd=pwd, month=month, salary=salary))
while tag:
select = input('请输入查询序号\n1:查询总工资\n2:查询用户身份\n3:退出登录\n')
if select.isdigit() and int(select) == 1:
print('总工资是:', float(month)*float(salary))
elif select.isdigit() and int(select) == 2:
if user == 'alex': print('super user')
elif user == 'yuanhao' or user == 'wupeiqi':print('normal user')
else: print('unkown')
elif select.isdigit() and int(select) == 3: tag = False
else: print('请输入正确的选项序号(1,2,3)')
posted @ 2017-07-19 15:31  风火林山  阅读(109)  评论(0编辑  收藏  举报