计算机二级Python编程题记录(上)

简介: 计算机二级Python编程题记录

第一套


1




s = input("请输入一个字符串:")
print("{:*^30}".format(s))


2




a, b = 0, 1
while a<=50:
    print(a, end=',')
    a, b = b, a+b


3




import jieba
txt = input("请输入一段中文文本:")
ls= jieba.lcut(txt)
for i in ls[::-1]:
    print(i,end='')


4




import turtle
for i in range(3):
    turtle.seth(i*120)
    turtle.fd(100)


5




fo = open("PY202.txt", "w")
txt = input("请输入类型序列: ")
fruits = txt.split(" ")
d = {}
for fruit in fruits:
    d[fruit] = d.get(fruit, 0) + 1
ls = list(d.items())
ls.sort(key=lambda x: x[1], reverse=True)  # 按照数量排序
for k in ls:
    fo.write("{}:{}\n".format(k[0], k[1]))
fo.close()


6




fi = open("小女孩.txt", "r")
fo = open("PY301-1.txt", "w")
txt = fi.read()
d = {}
exclude = ", 。 ! ? 、 () 【】 《》 <> = : :+-*__“”..."
for word in txt:
    if word in exclude:
        continue
    else:
        d[word] = d.get(word, 0) + 1
ls = list(d.items())
ls.sort(key=lambda x:x[1], reverse=True)
fo.write("{}:{}".format(ls[0][0], ls[0][1]))
fo.close()
fi.close()


fi = open("小女孩.txt", "r")
fo = open("PY301-2.txt", "w")
txt = fi.read()
d = {}
for word in txt:
    d[word] = d.get(word, 0) + 1
del d["\n"]
ls = list(d.items())
ls.sort(key=lambda x: x[1], reverse=True)
for i in range(10):
    fo.write(ls[i][0])
fi.close()
fo.close()


fi = open("小女孩.txt", "r")
fo = open("小女孩-频次排序", "w")
txt = fi.read()
d = {}
for word in txt:
    d[word] = d.get(word, 0) + 1
del d[" "]
del d["\n"]
ls = list(d.items())
ls.sort(key=lambda x: x[1], reverse=True)  # 此行可以按照词频由高到低排序
fo.write(",".join(ls))
fo.close()
fi.close()


第二套


1




import random
brandlist = ['三星','苹果','vivo','OPPO','魅族']
random.seed(0)
name = brandlist[random.randint(0,4)]
print(name)


2



import jieba
s = input("请输入一个字符串")
n = len(s) 
m = len(jieba.lcut(s))
print("中文字符数为{},中文词语数为{}。".format(n, m))


3




n = eval(input("请输入数量:"))
if n == 1:
    cost = 150
elif n >= 2 and n <= 3:
    cost = int(n * 150 * 0.9)
elif n >= 4 and n <= 9:
    cost = int(n * 150 * 0.8)
elif n >= 10:
    cost = int(n * 150 * 0.7)
print("总额为:", cost)


4



from turtle import *
for i in range(5):
   fd(200)
   right(144)


5




fo = open("PY202.txt", "w")
data = input("请输入一组人员的姓名、性别、年龄:")  # 姓名 年龄 性别
women_num = 0
age_amount = 0
person_num = 0
while data:
    name, sex, age = data.split(' ')
    if sex == '女':
        women_num += 1
    age_amount += int(age)
    person_num += 1
    data = input("请输入一组人员的姓名、性别、年龄:")
average_age = age_amount / person_num
fo.write("平均年龄是{:.1f} 女性人数是{}".format(average_age, women_num))
fo.close()


6




fi = open("PY301-vacations.csv", "r")
ls = []
for line in fi:
    ls.append(line.strip("\n").split(","))
s = input("请输入节假日名称:")
for line in ls:
    if s == line[1]:
        print("{}的假期位于{}-{}之间".format(line[1], line[2], line[3]))
fi.close()


fi = open("PY301-vacations.csv","r")
ls = []
for line in fi:
    ls.append(line.strip("\n").split(","))
s = input("请输入节假日序号:").split(" ")
while True:
    for i in s:
        for line in ls:
            if i == line[0]:
                print("{}({})假期是{}月{}日至{}月{}日之间".format((line[1]),(line[0]),line[2][:-2],line[2][-2:],line[3][:-2],line[3][-2:]))
    s = input("请输入节假日序号:").split(" ")
fi.close()


fi = open("PY301-vacations.csv","r")
ls = []
for line in fi:
    ls.append(line.strip("\n").split(","))
s = input("请输入节假日序号:").split(" ")
while True:
      for i in s:
            flag = False
            for line in ls:
                  if i == line[0]:
                        print("{}({})假期是{}月{}日至{}月{}日之间".format((line[1]),(line[0]),line[2][:-2],line[2][-2:],line[3][:-2],line[3][-2:]))
                        flag = True
            if flag == False:
                  print("输入节假日编号有误!")
      s = input("请输入节假日序号:").split(" ")
fi.close()


第三套


1




n = eval(input("请输入正整数:"))
print("{:@>30,}".format(n))


2




a = [11, 3, 8]
b = eval(input())  # 例如:[4,5,2]
s = 0
for i in range(3):
    s += a[i] * b[i]
print(s)


3




import random
random.seed(255)
for i in range(5):
    print(random.randint(1,50), end=" ")


4




import turtle
turtle.pensize(2)
d = 72
for i in range(5):
    turtle.seth(d)
    d += 72
    turtle.fd(200)


5




fo = open("PY202.txt", "w")
names = input("请输入各个同学行业名称,行业名称之间用空格间隔(回车结束输入):")
names = names.split(" ")
d = {}
for name in names:
    d[name] = d.get(name, 0) + 1
ls = list(d.items())
ls.sort(key=lambda x: x[1], reverse=True)  # 按照数量排序
for k in ls:
    fo.write("{}:{}\n".format(k[0], k[1]))
fo.close()


6




fi = open("论语.txt", "r")
fo = open("论语-原文.txt", "w")
flag = False
for line in fi:
    if "【" in line:
        flag = False
    if "【原文】" in line:
        flag = True
        continue
    if flag == True:
        fo.write(line.lstrip())
fi.close()
fo.close()


fi = open("论语-原文.txt", ______)
fo = open("论语-提纯原文.txt", ______)
for line in fi:
    for i in range(1,23):
        line=line.replace("({})".format(i), "")
    fo.write(line)
fi.close()
fo.close()


第四套


1




ntxt = input("请输入4个数字(空格分隔):")
nls = ntxt.split(" ")
x0 = eval(nls[0])
y0 = eval(nls[1])
x1 = eval(nls[2])
y1 = eval(nls[3])
r = pow(pow(x1-x0, 2) + pow(y1-y0, 2), 0.5)
print("{:.1f}".format(r))


2




import jieba
txt = input("请输入一段中文文本:")
ls = jieba.lcut(txt)
print("{:.1f}".format(len(txt)/len(ls)))


3




n = eval(input("请输入一个数字:"))
print("{:+^11}".format(chr(n-1)+chr(n)+chr(n+1)))


4




import turtle
d = 0
for i in range(4):
    turtle.fd(200)
    d = d+90
    turtle.seth(d)


5




fo = open("PY202.txt", "w")
data = input("请输入课程名及对应的成绩:")  # 课程名 考分
course_score_dict = {}
while data:
    course, score = data.split(" ")
    course_score_dict[course] = eval(score)
    data = input("请输入课程名及对应的成绩:")
course_list = sorted(list(course_score_dict.values()))
max_score, min_score = course_list[-1], course_list[0]
average_score = sum(course_list) / len(course_list)
max_course, min_course = '', ''
for item in course_score_dict.items():
    if item[1] == max_score:
        max_course = item[0]
    if item[1] == min_score:
        min_course = item[0]
fo.write("最高分课程是{} {}, 最低分课程是{} {}, 平均分是{:.2f}".format(max_course, max_score, min_course, min_score, average_score))
fo.close()


6



fi = open("sensor.txt", "r")
fo = open("earpa001.txt", "w")
txt = fi.readlines()
for line in txt:
    ls = line.strip("\n").split(",")
    if " earpa001" in ls:
        fo.write('{},{},{},{}\n'.format(ls[0], ls[1], ls[2], ls[3]))
fi.close()
fo.close()


fi = open("earpa001.txt", "r")
fo = open("earpa001_count.txt", "w")
d = {}
for line in fi:
    split_data = line.strip("\n").split(",")
    floor_and_area = split_data[-2] + "-" + split_data[-1]
    d[floor_and_area] = d.get(floor_and_area, 0) + 1
    # if floor_and_area in d:
    #     d[floor_and_area] += 1
    # else:
    #     d[floor_and_area] = 1
ls = list(d.items())
ls.sort(key=lambda x: x[1], reverse=True)  # 该语句用于排序
for i in range(len(ls)):
    fo.write('{},{}\n'.format(ls[i][0], ls[i][1]))
fi.close()
fo.close()


第五套




s = eval(input("请输入一个数字:"))
ls = [0]
for i in range(65,91):
    ls.append(chr(i))
print("输出大写字母:{}".format(ls[s]))


2




s = input("请输入一个十进制数:")
num = int(s)
print("转换成二进制数是:{:b}".format(num))


3




import jieba
s = input("请输入一个中文字符串,包含标点符号:")
m = jieba.lcut(s)
print("中文词语数:{}".format(len(m)))


4




import turtle
turtle.color("red","yellow")
turtle.begin_fill()
for i in range(36):
    turtle.fd(200)
    turtle.left(170)
turtle.end_fill()


5




fo = open("PY202.txt","w")
def prime(num):
    #此处可以是多行代码
    for i in range(2, num):
        if num%i==0:
            return False
    return True
ls = [51,33,54,56,67,88,431,111,141,72,45,2,78,13,15,5,69]
lis = []
for i in ls:
    if prime(i) == False:
        lis.append(i)#此处为一行代码
fo.write(">>>{},列表长度为{}".format(lis,len(lis)))
fo.close()


6




fi = open("arrogant.txt","r")
fo = open("PY301-1.txt","w")
txt = fi.read()
d = {}
for s in txt:
    d[s] = d.get(s,0)+1
del d["\n"]
ls =list(d.items())
for i in range(len(ls)):
    fo.write("{}:{}\n".format(ls[i][0],ls[i][1]))
fo.close()
fi.close()


fi = open("arrogant.txt","r")
fo = open("arrogant-sort.txt","w")
txt = fi.read()
d = {}
for s in txt:
    d[s] = d.get(s,0)+1
del d["\n"]
ls =list(d.items())
ls.sort(key=lambda x:x[1],reverse=True)
for i in range(10):
    fo.write("{}:{}\n".format(ls[i][0],ls[i][1]))
fi.close()
fo.close()


目录
相关文章
|
8天前
|
数据挖掘 数据处理 Python
Python编程入门:从基础到实践
【6月更文挑战第26天】这篇文章引导读者逐步学习Python编程,从基础语法如变量、数据类型(整数、浮点数、字符串)到条件语句、循环(if/for/while),再到函数定义和模块导入。通过实例展示了Python在文本处理、数据分析(使用pandas)和Web开发(使用Flask)的应用。学习Python能为初学者开启更广阔的技术领域,如面向对象编程、并发和网络编程等。
|
6天前
|
设计模式 程序员 测试技术
老程序员分享:Python数据模型及Pythonic编程
老程序员分享:Python数据模型及Pythonic编程
17 1
|
14小时前
|
语音技术 开发者 Python
语音识别,python运行H ~W~,要使用英符,执行Python的流程是输入Python,回车,解释器的两大功能,翻译代码,提交计算机运算,多行代码运行,写一个py文件,pycharm安
语音识别,python运行H ~W~,要使用英符,执行Python的流程是输入Python,回车,解释器的两大功能,翻译代码,提交计算机运算,多行代码运行,写一个py文件,pycharm安
|
16小时前
|
机器学习/深度学习 人工智能 算法
【坚果识别】果实识别+图像识别系统+Python+计算机课设+人工智能课设+卷积算法
坚果识别系统,使用Python语言进行开发,通过TensorFlow搭建卷积神经网络算法模型,对10种坚果果实('杏仁', '巴西坚果', '腰果', '椰子', '榛子', '夏威夷果', '山核桃', '松子', '开心果', '核桃')等图片数据集进行训练,得到一个识别精度较高的模型文件,让后使用Django搭建Web网页端界面操作平台,实现用户上传一张坚果图片 识别其名称。
3 0
|
1天前
|
大数据 程序员 Python
Python数据类型大变身!掌握列表推导式与生成器,编程效率翻倍不是梦
【7月更文挑战第2天】在Python中,列表推导式和生成器是提升效率的利器。列表推导式以简洁方式处理循环和条件,如将偶数平方化简为一行代码,提高代码可读性。生成器则按需生成数据,减少内存占用,适合处理大数据。通过`yield`函数实现惰性求值,有效避免内存溢出。掌握这两者,能优化Python编程体验。
|
2天前
|
安全 数据处理 调度
Python多线程编程详解
Python多线程编程详解
|
2天前
|
Java UED Python
Python多线程编程实战技巧与性能优化策略
Python多线程编程实战技巧与性能优化策略
|
7天前
|
索引 Python 容器
Python数据类型:编程新手的必修课
Python数据类型:编程新手的必修课
|
7天前
|
算法 Java 程序员
Python面相对象的编程
Python面相对象的编程
|
8天前
|
Python
揭秘Python安装目录:你的编程宝库隐藏了哪些宝藏?
揭秘Python安装目录:你的编程宝库隐藏了哪些宝藏?

相关实验场景

更多