8/8/2018 Python程序设计群作业----Python语言测试练习题【第十二天】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tobe_numberone/article/details/81502953

前言

本群的作业,没有指定Python方向,而在于提升群内成员的语言功底,以便大家在Python的其他方向走的更远。
本群欢迎任何程度的Python学习者
Python程序设计 群号:651707058

题一:题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

for i in range(1,5):
    for j in range(1,5):
        for k in range(1,5):
            if i!=j!=k:
                print(f'{i}{j}{k}')

题二:对应相加

[[‘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)
    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个激活码,他们各不相同

def creat_key(key_num):
    '''
    :param key_num: 激活码的数量 
    :return: list[str] 放有激活码的列表
    '''
    from random import randint
    res = set()
    while True:    #一共生成激活码的次数
        if len(res) == 200:
            break
        key = ''        #空激活码
        for i in range(16): #激活码一共16个字符(除去分隔符)
            if i %4==0:
                key+='-'
            choice = randint(1,5)   #1->随机出一个数字,[2,3]->随机出一个大写字母,[4,5]->随机出一个小写字母
            if choice == 1:
                key += str(randint(0,9))
            elif 1<choice<=3:
                key += chr(randint(ord('A'),ord('Z')))
            else:
                key += chr(randint(ord('a'), ord('z')))
        res.add(key[1:])#去掉开头的分隔符
    return res
res = creat_key(200)
print(res)

题四:(困难)设计一个词汇量测试软件

文件中有高中英语单词词汇excel,需要使用文件操作,将文件内容读取出来,放入一个你认为
不错的数据类型中
程序功能1:让用户输入需要考查单词的数量(数量必须20个以及上)
功能2:随机出一个单词,给出ABCD 4个汉语意思,一个正确的,三个错误的
功能3:用户输入ABCD进行选择相应选项
功能4:用户选择后,代码需要判断正误,立即给出正确或错误,并给出正确答案
功能5:单词数量测试完毕后,按这种格式输出:测试数量:100 正确:70 错误:30 正确率:70%
功能6:提示用户是否需要查看选错的单词,是->列出错误的单词,否->退出程序
Excel 文件下载地址:点击打开

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

猜你喜欢

转载自blog.csdn.net/tobe_numberone/article/details/81502953