第一套
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()