p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-size: 10.5000pt }
p.pre { margin: 0pt; margin-bottom: .0001pt; text-align: left; font-family: 宋体; font-size: 12.0000pt }
span.msoIns { text-decoration: underline; color: blue }
span.msoDel { text-decoration: line-through; color: red }
div.Section0 { }
1:编写for循环,利用索引遍历出每一个字符
msg=‘hello egon 666‘
msg=‘hello egon 666‘
for work1 in range(0,len(msg)):
print(msg[work1])
2:编写while循环,利用索引遍历出每一个字符
msg=‘hello egon 666‘
msg=‘hello egon 666‘
work2 = 0
while work2 < len(msg):
print(msg[work2])
work2 += 1
3:
msg=‘hello alex‘中的alex替换成SB
msg=‘hello alex‘
work3 = msg.replace(‘alex‘,‘SB‘)
print(work3)
4:
msg=‘/etc/a.txt|365|get‘
将该字符的文件名,文件大小,操作方法切割出来
msg=‘/etc/a.txt|365|get‘
work4 = msg.split(‘|‘)
print(work4)
5.编写while循环,要求用户输入命令,如果命令为空,则继续输入
tag5 = True
while tag5:
cmd5 = input(‘==>‘)
if cmd5 == ‘q‘:
tag5 = False
continue
print(cmd5)
6.编写while循环,让用户输入用户名和密码,如果用户为空或者数字,则重新输入
tag6 = True
while tag6:
usr6_inp = input(‘请输入用户名:‘)
pass6_inp = input(‘请输入命令:‘)
if usr6_inp == ‘‘ or usr6_inp.isdigit():
continue
else:
print(‘登录成功,%s!‘%(usr6_inp))
tag6 = False
7.编写while循环,让用户输入内容,判断输入的内容以alex开头的,则将该字符串加上_SB结尾
tag7 = True
while tag7:
work7 = input(‘请输入用户名:‘)
if work7 == ‘alex‘:
print(work7+‘_SB‘)
tag7 = False
continue
else:
print(work7+‘你好!‘)
break
8.
1.两层while循环,外层的while循环,让用户输入用户名、密码、工作了几个月、每月的工资(整数),用户名或密码为空,或者工作的月数不为整数,或者
月工资不为整数,则重新输入
2.认证成功,进入下一层while循环,打印命令提示,有查询总工资,查询用户身份(如果用户名为alex则打印super user,如果用户名为yuanhao或者wupeiqi
则打印normal user,其余情况均打印unkown user),退出功能
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
tag8 = True
while tag8:
user_inp = input(‘请输入用户名:‘)
if user_inp == ‘‘:
print(‘输入错误,请重新输入!‘)
continue
password_inp = input(‘请输入密码:‘)
if password_inp == ‘‘:
print(‘输入错误,请重新输入!‘)
continue
work_mons_inp = input(‘请输入工作的月份‘)
if not work_mons_inp.isdigit():
print(‘输入错误,请重新输入!‘)
continue
salary_inp = input(‘请输入月薪‘)
if not salary_inp.isdigit():
print(‘输入错误,请重新输入!‘)
continue
print(‘user:‘ + user_inp)
print(‘password:‘ + password_inp)
print(‘work_mons:‘ + work_mons_inp)
print(‘salary:‘ + salary_inp)
while tag8:
choice_inp = input(‘请输入你要执行的操作:(1、查询总工资 2、查询用户身份 3、退出)‘)
if choice_inp == ‘1‘:
work_mons_inp = int(work_mons_inp)
salary_inp = int(salary_inp)
sum_salary = work_mons_inp * salary_inp
print(sum_salary)
elif choice_inp == ‘2‘:
if user_inp == ‘alex‘:
user_id = ‘super user‘
elif user_inp == ‘wupeiqi‘ or user_inp == ‘yuanhao‘:
user_id = ‘normal user‘
else:
user_id = ‘unknowuser‘
print(user_id)
elif choice_inp == ‘3‘:
print(‘欢迎使用,下次再见!‘)
tag8 = False