Day12(Python作业)
(Python作业来自qq群:651707058,欢迎任何程度的Python学习者)
题一:数字相加
[[‘A’,’1’],[‘B’,’2’], [‘C’,’3’], [‘A’,’4’], [‘B’,’5’], [‘C’,’6’], [‘A’,’1’], [‘B’,’1’], [‘C’,’1’]]
分别将这里面的A所对应的数都相加,B所对应的数相加,C所对应的数相加(能力好的请不要去自己去数列表中有哪些字母,要认为这里面的A,B,C是随机的 有可能下回说不定就是G,H,I了)
def num_add(L):
'''
:param L:list[list] 原数据
:return: dict 字典 字母为key 数为value
'''
res = {}
for i,v in L:
if i not in res:
res[i] = int(v)
else:
res[i]+=int(v)
#print(i,v)可以理解一下for i,v in L:的意义
return res
L = [['A','1'],['B','2'], ['C','3'], ['A','4'], ['B','5'], ['C','6'], ['A','1'], ['B','1'], ['C','1']]
print(num_add(L))
题二:激活码生成器
假如你要为一个应用搞限时促销,生成激活码(或者优惠券),请用 Python 如何生成 200 个激活码(或者优惠券)。激活码的格式为asqE-9xRK-lqWU-QkMT
要求1: 使用随机生成时,生成数字概率为1/5,大写字母和小写字母概率各为2/5
要求2: 200个激活码,他们各不相同
import random
def creat_key(key_num):
'''
:param key_num: 激活码的数量
:return: list[str] 放有激活码的列表
'''
counter=0
s=''#空激活码
for k in range(0,key_num):#共多少个激活码
for j in range(0,16):#激活码长度
i = random.randint(1, 5)#实现字母和数字出现概率
while True:
if i==1:
s+=str(random.randint(0,9))
break
elif i==2 or i==3:
s+=chr(random.randint(65,90))
break
else:
s+=chr(random.randint(97,122))
break
if j%4==0 and j%16!=0:
s+='-'
if j%16==0:
s+='\n'
print(s)
creat_key(20)
题三:(比较简单)有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
L=[1,2,3,4]
for i in L:
for j in L:
for k in L:
if i!=j!=k:
print(i*100+j*10+k,end=' ')
题四:(较难,不会,下面是一个大佬的解答)设计一个词汇量测试软件
文件中有高中英语单词词汇excel,需要使用文件操作,将文件内容读取出来,放入一个你认为
不错的数据类型中
程序功能1:让用户输入需要考查单词的数量(数量必须20个以及上)
功能2:随机出一个单词,给出ABCD 4个汉语意思,一个正确的,三个错误的
功能3:用户输入ABCD进行选择相应选项
功能4:用户选择后,代码需要判断正误,立即给出正确或错误,并给出正确答案
功能5:单词数量测试完毕后,按这种格式输出:测试数量:100 正确:70 错误:30 正确率:70%
功能6:提示用户是否需要查看选错的单词,是->列出错误的单词,否->退出程序
Excel 文件下载地址:http://en2.eywedu.net/submit/abShowSoft.asp?UrlID=1&SoftID=44021
from random import sample,randint
import xlrd
def check_answer(right):
answer = input('选择:')
return right == answer or right.lower() == answer
excel = xlrd.open_workbook('高中3500个英语单词表.xls')
sheet1 = excel.sheet_by_index(3)
words = sheet1.col_values(1,1)
meanings =sheet1.col_values(3,1)
amount = int(input('要考查单词的数量:'))
if amount<20:
pass
sample_words_meanings = sample(tuple(zip(words,meanings)),amount)
i = 0
wrong = 0
for w,m in sample_words_meanings: #w->word m->meaning
print('\n(',str(i+1).ljust(1),').',w.ljust(10))
answers = sample(meanings,3)
position = randint(0,3)
answers.insert(position,m)
choice = ['A','B','C','D']
for ch,an in zip(choice,answers):
# an = an.split('.')[1:]
# an = ''.join(an)
print(f'<{ch.ljust(1)}>',' '*2,an)
if check_answer(choice[position]):
print('正确')
else:
print('错误')
wrong+=1
i+=1
学习愉快!!