首页 > 代码库 > day16作业
day16作业
work1
user1_inp = input(‘please input username: ‘)
pass1_inp = input(‘please input your password: ‘)
if user1_inp == ‘seven‘ and pass1_inp == ‘123‘:
print(‘login success‘)
else:
print(‘login failed‘)
for work_1_b in range(0, 3):
user2_inp = input(‘please input username: ‘)
pass2_inp = input(‘please input your password: ‘)
if user2_inp == ‘seven‘ and pass2_inp == ‘123‘:
print(‘login success‘)
else:
print(‘login failed‘)
for work_1_c in range(0, 3):
user3_inp = input(‘please input username: ‘)
pass3_inp = input(‘please input your password: ‘)
if (user3_inp == ‘alex‘ or ‘seven‘) and pass3_inp:
print(‘login success‘)
else:
print(‘login failed‘)
work2
v2a = 0
work2_a = 1
while work2_a < 100:
work2_a += 1
if work2_a % 2 == 1:
v2a = v2a - work2_a
else:
v2a = v2a + work2_a
print(v2a)
v2b = 0
for work2_b in range(1,100):
if work2_b % 2 == 0:
work2_b = -work2_b
v2b = v2b + work2_b
print(v2b)
v2c = 0
while v2c < 12:
v2c = v2c + 1
if v2c == 6 or v2c == 10:
continue
print(v2c)
v2d = 0
while v2d < 100:
v2d = v2d + 1
if v2d % 2 == 1:
print(v2d)
v2e = 0
while v2e < 100:
v2e = v2e + 1
if v2e % 2 == 0:
print(v2e)
work3
name = ‘ alex ‘
v3a = name.strip()
print(v3a)
name = ‘alex‘
v3b = name.startswith(‘al‘)
print(v3b)
name = ‘alex‘
v3c = name.endswith(‘x‘)
print(v3c)
name = ‘alex‘
v3d = name.replace(‘l‘, ‘p‘)
print(v3d)
name = ‘alex‘
v3e = name.split(‘l‘)
print(v3e)
v3f = print(type(v3e)) # 分割后的数据类型是列表
name = ‘alex‘
v3g = name.upper()
print(v3g)
name = ‘Alex‘
v3h = name.lower()
print(v3h)
name = ‘alex‘
v3i = name[1]
print(v3i)
name = ‘alex‘
v3j = name[0:3]
print(v3j)
name = ‘alex‘
v3k = name[-2:]
print(v3k)
name = ‘alex‘
v3l = name.find(‘e‘)
print(v3l)
work4
li = [‘alex‘, ‘eric‘, ‘rain‘]
v4a = len(li)
print(v4a)
li = [‘alex‘, ‘eric‘, ‘rain‘]
li.append(‘seven‘)
print(li)
li = [‘alex‘, ‘eric‘, ‘rain‘, ‘seven‘]
li.insert(1,‘Tony‘)
print(li)
li = [‘alex‘, ‘eric‘, ‘rain‘, ‘seven‘]
li[1] = ‘Kelly‘
print(li)
li = [‘alex‘, ‘eric‘, ‘rain‘, ‘seven‘]
li.remove(‘eric‘)
print(li)
li = [‘alex‘, ‘eric‘, ‘rain‘, ‘seven‘]
li_f = li.pop(1)
print(li_f)
print(li)
li = [‘alex‘, ‘eric‘, ‘rain‘, ‘seven‘]
li.pop(2)
print(li)
li = [‘alex‘, ‘eric‘, ‘rain‘, ‘seven‘]
del li[1:4]
print(li)
li = [‘alex‘, ‘eric‘, ‘rain‘, ‘seven‘]
li.reverse()
print(li)
work5
for1 = ‘*‘
for i in range(9):
print(for1)
for1 += ‘*‘
work6
for2 = ‘*‘
for i in range(9,0,-1):
print(for2*i)
work7
for3 = ‘*‘
for i in range(0,9):
print(for3.center(20))
for3 += ‘**‘
work8
#去掉重复
names=[‘egon‘,‘alex‘,‘egon‘,‘wupeiqi‘]
names = [‘egon‘, ‘alex‘, ‘egon‘, ‘wupeiqi‘]
names = list(set(names))
print(names)
#去掉重复,且保证列表顺序与原来保持一致
names=[‘egon‘,‘alex‘,‘egon‘,‘wupeiqi‘]
names = [‘egon‘, ‘alex‘, ‘egon‘, ‘wupeiqi‘]
names1 = set(names)
names2 = []
for i in names:
if i in names1 and i not in names2:
names2.append(i)
names = names2
另一种简化方案
names = [‘egon‘, ‘alex‘, ‘egon‘, ‘wupeiqi‘]
names2 = []
for i in names:
if i not in names2:
names2.append(i)
print(names2)
#去掉重复,且保证列表顺序与原来保持一致
names=[[1,2],3,[1,2],4]
names = [[1, 2], 3, [1, 2], 4]
names3 = []
for i in names:
if i not in names3:
names3.append(i)
print(names3)
work9
#统计s=‘hello alex alex say hello sb sb‘中每个单词的个数
s = ‘hello alex alex say hello sb sb‘
s_l = s.split()
s_L_s = set(s_l)
for i9 in s_L_s:
print(‘%s的个数是%s‘,(i9,s_l.count(i9)))
work10
if True or False and False:
print(‘yes‘)
else:
print(‘no‘)
#输出结果为?为什么?
输出yes,因为逻辑或判断,从前向后开始,遇到一个True即为真,判断结束
if (True or False) and False:
print(‘yes‘)
else:
print(‘no‘)
#输出结果为?为什么?
输出no,先判断括号里面的真假,再和括号外面的逻辑运算
work11
编写if多分支,猜老男孩的年纪
oldboy_age_guess = int(input(‘请猜老男孩的年龄? ‘))
if oldboy_age_guess > 50:
print(‘猜大了!‘)
elif oldboy_age_guess < 50:
print(‘猜小了!‘)
elif oldboy_age_guess == 50:
print(‘猜对了!‘)
work12
用户输入用户名密码验证,验证通过后进入子循环,输入命令,命令若为q,则退出所有循环
usr_inp = input(‘请输入用户名:‘)
pas_inp = input(‘请输入密码: ‘)
if usr_inp == ‘alex‘ and pas_inp == ‘123‘:
tag12 = True
while tag12:
cmd_12 = input(‘请输入你要执行的命令(按q退出): ‘)
if cmd_12 == ‘q‘:
tag12 = False
continue
print(‘====> ‘+cmd_12)
work13
循环取出元组中所有元素:方式一:while和for(按照索引),方式二:不按照索引的方式
# t=(1,2,3,4,5,6,7,8,9)
work13_while = 0
while work13_while < len(t):
print(t[work13_while])
work13_while += 1
for work13_for in range(len(t)):
print(t[work13_for])
for work13_no_index in t:
print(work13_no_index)
work14
循环读取列表以及子列表中所有元素
l=[1,2,[3,4],[5,6]]
for work14_1 in l:
if type(work14_1) is list:
for work14_2 in work14_1:
print(work14_2)
else:
print(work14_1)
work15
基础需求:
让用户输入用户名密码
认证成功后显示欢迎信息
输错三次后退出程序
err_time = 0
while err_time < 3:
usr_inp = input(‘请输入用户名: ‘)
pas_inp = input(‘请输入密码: ‘)
if usr_inp == ‘alex‘ and pas_inp == ‘123‘:
print(‘登陆成功!‘)
break
else:
err_time += 1
if err_time == 3:
print(‘登陆错误3次,请稍后尝试!‘)
break
print(‘登陆失败,请重新输入‘)
work16
有十进制数n=10
转成二进制
转成八进制
转成十六进制
n = 10
n_2 = bin(n)
print(n_2)
n_8 = oct(n)
print(n_8)
n_16 = hex(n)
print(n_16)
work17
与用户交互,要求用户输入年龄和薪资,将用户输入的年龄转成整形,将用户输入的薪资转成浮点型
age_inp = int(input(‘请输入你的年龄: ‘))
salary_inp = float(input(‘请输入你的薪水: ‘))
print(‘你的年龄是%s岁,你的薪水是%s元‘ % (age_inp, salary_inp))
work18
用户输入用户名,年纪,工作,爱好,格式化输出如下内容(使用%s和format两种方式)
------------ info of Alex Li -----------
Name : Alex Li
Age : 22
job : Teacher
Hobbie: girl
------------- end -----------------
name_inp = input(‘请输入你的名字: ‘)
age_inp = input(‘请输入你的年龄: ‘)
job_inp = input(‘请输入你的工作: ‘)
hobby_inp = input(‘请输入你的爱好: ‘)
print(‘-------- info of %s --------‘ % (name_inp))
print(‘Name: %s‘ % (name_inp))
print(‘Age: %s‘ % (age_inp))
print(‘Job: %s‘ % (job_inp))
print(‘Hobbie %s‘ % (hobby_inp))
print(‘-------- end --------‘)
work19
s=‘alex say hello‘
切片取出第say
切片取出倒数后两个字符
s=‘alex say hello‘
print(s[5:8])
print(s[-2:])
work20
# 编写循环,让用户输入年纪,如果输入为空,或者不为数字,则重新输入
while True:
age_inp = input(‘请输入你的年龄: ‘)
if age_inp == ‘‘ or not age_inp.isdigit():
print(‘输入错误,请重新输入!‘)
continue
print(‘你的年龄是%s岁‘%(age_inp))
break
work21
用列表模拟上电梯的流程(队列)
循环生成一个1000个值的列表(入队)
循环取走这个1000个值(出队)
work21_1 = []
for v21_1 in range (1000):
work21_1.append(v21_1)
print(work21_1)
for v21_1 in range (1000):
work21_1.pop(0)
print(work21_1)
用列表模拟把衣服放箱子里,然后取衣服的流程(堆栈)
循环生成一个1000个值的列表(入栈)
循环取走这个1000个值(出栈)
work21_2 = []
for v21_2 in range (1000):
work21_2.append(v21_2)
print(work21_2)
for v21_2 in range (1000):
work21_2.pop()
print(work21_2)
work22
dicta={‘a‘:1,‘b‘:2,‘c‘:3,‘d‘:‘hello‘}
dictb={‘b‘:3,‘c‘:2,‘d‘:‘world‘,‘f‘:10}
#两字典相加,不同的key对应的值保留,相同的key对应的值相加后保留,如果是字符串就拼接(字符串拼接‘hello‘+‘world‘得‘helloworld‘)
# {‘a‘: 1, ‘b‘: 5, ‘c‘: 5, ‘d‘: ‘helloworld‘, ‘f‘: 10}
dicta = {‘a‘: 1, ‘b‘: 2, ‘c‘: 3, ‘d‘: ‘hello‘}
dictb = {‘b‘: 3, ‘c‘: 2, ‘d‘: ‘world‘, ‘f‘: 10}
dictc = {}
set_a = set(dicta)
set_b = set(dictb)
set_jiaoji = set_a & set_b
set_only_a = set_a - set_b
set_only_b = set_b - set_a
for i22 in set_only_a:
dictc[i22] = dicta[i22]
print(dictc)
for i22 in set_only_b:
dictc[i22] = dictb[i22]
print(dictc)
for i22 in set_jiaoji:
dictc[i22] = dicta[i22]+dictb[i22]
print(dictc)
work23
#练习十二:
取出列表中的名字,年龄,出生的年,月,日
data=http://www.mamicode.com/[‘alex‘,49,[1900,3,18]]
data=[‘alex‘,49,[1900,3,18]]
for i23 in data:
if type(i23) is list:
for i23_1 in i23:
print(i23_1)
else:
print(i23)
work24
题目没有布置完,没法做
字典嵌套使用
#alex发表了朋友圈:
alex_msg=‘‘‘
我想淫一首小诗
歌颂我的同桌
她乌黑的头发
好像一直炸毛鸡
‘‘‘
#eogn发表了朋友圈:
egon_msg=‘‘‘
总有文盲装文艺
‘‘‘
comments={
alex_msg:{
‘author‘:‘alex‘,
‘comments‘:[
(‘wupeiqi‘,‘真傻比‘),
(‘元昊‘,‘确实傻比‘),
(‘egon‘,‘同上‘)
],
‘thumb_counts‘:3,
},
egon_msg:{
‘author‘:‘egon‘,
‘comments‘:[
(‘alex‘,‘说谁呢‘),
(‘元昊‘,‘你被开除了‘),
(‘wupeiqi‘,‘好刺激‘)
],
‘thumb_counts‘:2,
},
}
day16作业