python入门经典例题

第一题:计算不重复数字

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

for i in [1,2,3,4]:
    for j in [1,2,3,4]:
        for k in [1,2,3,4]:
            print(i*100+j*10+k)

法二:

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了
”’
法一:

L=[['A','1'],['B','2'], ['C','3'], ['A','4'], ['B','5'], ['C','6'], ['A','1'], ['B','1'], ['C','1']]
x1=0
x2=0
x3=0
for i in L:
   if i[0]=='A':
       x1+=int(i[1])
   elif i[0]=='B':
       x2+=int(i[1])
   else:
       x3+=int(i[1])
print(x1,x2,x3)

法二:

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个激活码,他们各不相同
”’
参考大佬的:https://blog.csdn.net/tobe_numberone/article/details/81485054

def creat_key(key_num):
    '''
    :param key_num: 激活码的数量 
    :return: list[str] 放有激活码的列表
    '''
    from random import randint
    res = []
    for t in range(key_num):    #一共生成激活码的次数
        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.append(key[1:])#去掉开头的分隔符
    return res
res = creat_key(200)
print(res)

第四题:词汇量测试

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

ps:文件中有7个文件,其中一个是高中总汇单词,其余是分年级的单词。有兴趣的话,可以让用户选择某个年级
的单词来测试
”’
参考大佬的:https://blog.csdn.net/tobe_numberone/article/details/81485054

import re
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
print('测试数量:',amount,'正确:',amount-wrong,'错误:',wrong,'正确率:',((amount-wrong)/amount)*100,'%')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值