一丶上课内容
#默写代码while部分
while True:
name=input('please input your name: ')
password=input('please input your password: ')
if name == 'egon' and password == '123':
print('login successfull')
while True:
cmd=input('>>: ')
if cmd == 'quit':
break
print('====>',cmd)
break
tag=True
while tag:
name=input('please input your name: ')
password=input('please input your password: ')
if name == 'egon' and password == '123':
print('login successfull')
while tag:
cmd=input('>>: ')
# if cmd == 'quit':
# tag=False
# continue
# print('====>',cmd)
if cmd == 'quit':
tag=False
else:
print('====>',cmd)
count=0
while count < 10:
if count == 3:
count+=1
continue
print(count)
count+=1
else: #最后执行
print('在最后执行,并且只有在while循环没有被break打断的情况下才执行')
#字符串部分
#一:移除空白
while True:
name=input('user: ').strip()
password=input('password: ').strip()
if name == 'egon' and password == '123':
print('login successfull')
#二:从字符串中切出用户命令的详细信息
while True:
cmd=input('>>: ').strip()
if len(cmd) == 0:continue
cmd_l=cmd.split()
print('命令是:%s 命令的参数是:%s' %(cmd_l[0],cmd_l[1]))
#三:判断用户输入的是否为整型数字,如果是,则转换成整型
oldboy_age=84
while True:
age=input('>>: ').strip()
if len(age) == 0:continue
if age.isdigit():
age=int(age)
else:
print('must be int')
#四:字符串格式化方法
print('my name is {name} my age is {age} my sex is {sex}'.format(
sex='male',
age=18,
name='egon'))
二丶作业
1:编写for循环,利用索引遍历出每一个字符(选做题)msg = 'hello egon 666'
for i in msg:
print(i)
2:编写while循环,利用索引遍历出每一个字符msg = 'hello egon 666'
while True:
a = int(len(msg))
b = a - 1
while True:
print(msg[b])
b -= 1
if b < 0:
break
break
第二种:
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'
# 将该字符的文件名,文件大小,操作方法切割出来
a = msg.split('/')
print(a)
b = a[2]
print(b)
c = b.split('|')
print(c[0])
print(c[2])
5.编写while循环,要求用户输入命令,如果命令为空,则继续输入
while True:
user = input('输入命令》》:')
if len(user) == 0:
print('输入为空,请重新输入:')
else:
print('>>>>>>>',user)
break
6.编写while循环,让用户输入用户名和密码,如果用户为空或者数字,则重新输入
while True:
username = input('用户名:》》》')
userpassword = input('密码:》》》')
if len(username) == 0:
print('输入的用户名不能为空,请重新输入:')
continue
elif username.isdigit() is True:
print('输入的用户名不能只是数字,请重新输入:')
continue
else:
print('登陆成功')
break
7.编写while循环,让用户输入内容,判断输入的内容以alex开头的,则将该字符串加上_SB结尾
while True:
userinput = input('请输入姓名:')
if userinput.startswith('alex') is True:
print(userinput.replace('alex','SB'))
break
else:
print(userinput)
8.1.
两层while循环,外层的while循环,让用户输入用户名、密码、工作了几个月、每月的工资(整数),用户名或密码为空,或者工作的月数不为整数,或者
月工资不为整数,则重新输入
2.
认证成功,进入下一层while循环,打印命令提示,有查询总工资,查询用户身份(如果用户名为alex则打印super
user,如果用户名为yuanhao或者wupeiqi
则打印normal
user,其余情况均打印unkown
user),退出功能
3.
要求用户输入退出,则退出所有循环(使用tag的方式)
tag = True
count = 0
while tag:
username = input('请输入用户名:')
userpassword = input('请输入密码:')
workings_hours = input('请输入工作时间:')
salary = input('请输入工资')
if len(username) == 0 or userpassword == 0:
print('用户名或密码输入不能为空,请重新输入:')
continue
elif workings_hours.isdigit() is False or salary.isdigit() is False:
print('输入的工作时间和工资必须为整数:')
elif True:
print('欢迎%s用户登陆成功'%(username.center(20,'*')))
print(('''
name:%s
workings_hours:%s
salary:%s
''')%(username,workings_hours,salary))
while tag:
a = 'Welcome'
print(a.center(30,'*'))
print('1查询总工资 2.查询用户身份 3.退出登录')
number = input('请输入要查询的信息编号:')
if number == '1':
gross_wage = int(workings_hours) * int(salary) #gross_wage是总工资
print('%s ,您好,您的总工资是%s'%(username,gross_wage))
while True:
quit_1 = input('查询完毕,返回上一层请按q,退出请按Q')
if quit_1 == 'Q':
tag = False
elif quit_1 == 'q':
break
else:
print('重新输入:')
continue
elif number == '2':
if username == 'alex':
print('尊敬的用户%s,您好,您是尊贵的superuser'%(username))
while True:
quit_2 = input('查询完毕,返回上一层请按q,退出请按Q')
if quit_2 == 'Q':
tag = False
elif quit_2 == 'q':
break
else:
print('您的输入错误,请重新输入')
continue
elif username == 'yuanhao' or username == 'wupeiqi':
print('尊敬的用户%s,您好,您是尊贵的normaluser' % (username))
quit_2 = input('查询完毕,返回上一层请按q,退出请按Q')
if quit_2 == 'Q':
tag = False
elif quit_2 == 'q':
continue
else:
print('您的输入错误,请重新输入')
continue
else:
print('unknow')
print('尊敬的用户您好,您不是我们这里的会员。自动帮您返回上一级')
continue
elif number == '3':
print('退出成功')
tag = False
else:
print('只能输入1,2,3')
continue