第一套
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())
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
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()
第六章
1
line = "After fresh rain in mountains bare "
print(line.title())
2
a = float(input("输入三角形第一条直角边长:"))
b = float(input("输入三角形第二条直角边长:"))
area = a*b*0.5
print("直角三角形的面积为:{:.1f}".format(area))
3
num = eval(input("输入数字:"))
print("对应的二进制数:{0:b}\n八进制数:{0:o}\n十六进制数:{0:X}".format(num))
4
import turtle
turtle.color('black','yellow')
turtle.begin_fill()
turtle.circle(50)
turtle.end_fill()
5
def judge_year(year):
if (year%4==0 and year%100!=0) or year%400==0:
print(year,"年是闰年")
else:
print(year, "年不是闰年")
year = eval(input("请输入年份:"))
judge_year(year)
6
fi = open("score.csv", "r")
fo = open("avg-score.txt", "w")
ls = []
x = []
sum = 0
for row in fi:
ls.append(row.strip("\n").split(","))
for line in ls[1:]:
for i in line[1:]:
sum = int(i) + sum
avg = sum / 3
x.append(avg)
sum = 0
fo.write("语文:{:.2f}\n数学:{:.2f}\n英语:{:.2f}\n物理:{:.2f}\n科学:{:.2f}".format(x[0], x[1], x[2], x[3], x[4]))
fi.close()
fo.close()
第七套
1
lis = [2,8,3,6,5,3,8]
new_lis = list(set(lis))
print(new_lis)
2
fruit = input('输入水果:')
lis = ['苹果','哈密瓜','橘子','猕猴桃','杨梅','西瓜']
if fruit in lis:
print(fruit+"在列表lis中")
else:
print(fruit+"不在列表lis中")
3
def str_change(str) :
return str[::-1]
str = input("输入字符串:")
print(str_change(str))
4
import turtle
turtle.color("black","yellow")
turtle.begin_fill()
for i in range(5):
turtle.fd(200)
turtle.right(144)
turtle.end_fill()
5
lower = int(input('输入区间最小值:'))
upper = int(input('输入区间最大值:'))
for num in range(lower+1,upper):
if num>1:
for i in range(2,num):
if(num%i)==0:
break
else:
print(num)
6
fo = open("PY301-1.txt","w")
class Horse():
def __init__(self, category, gender, age):
self.category = category
self.gender = gender
self.age = age
def get_descriptive(self):
self.info = "一匹" + self.category + str(self.age) + "岁的" + self.gender + "马"
def write_speed(self, new_speed):
self.speed = new_speed
addr = "在草原上奔跑的速度为"
fo.write(self.info + "," + addr + str(self.speed) + "km/h。")
horse = Horse("阿拉伯","公",12)
horse.get_descriptive()
horse.write_speed(50)
fo.close()
fo = open("PY301-2.txt","w")
class Horse():
def __init__(self, category, gender, age):
self.category = category
self.gender = gender
self.age = age
def get_descriptive(self):
self.info = "一匹" + self.category + str(self.age) + "岁的" + self.gender + "马"
def write_speed(self, new_speed):
self.speed = new_speed
addr = "在草原上奔跑的速度为"
fo.write(self.info + "," + addr + str(self.speed) + "km/h。")
class Camel(Horse):
def __init__(self, category, gender, age):
super().__init__(category, gender, age)
def write_speed(self,new_speed):
self.speed = new_speed
addr = "在沙漠上奔跑的速度为"
fo.write(self.info.replace("马","骆驼") + "," + addr + str(self.speed) + "km/h。")
camel = Camel("双峰驼","母",20)
camel.get_descriptive()
camel.write_speed(40)
fo.close()
第八套
1
animals = ['cow', 'duck', 'cat', 'dog']
animals.reverse()
print(animals)
2
word = " 窗前明月光,疑是地上霜。 "
print(word.strip())
3
count = 0
while count < 50:
count+=1
if count % 2 == 0:
continue
print(count,end=",")
4 ???
import turtle
for i in range(4):
turtle.seth(90*(i+1))
turtle.circle(50,90)
turtle.seth(90*(i-1))
turtle.circle(50,90)
turtle.hideturtle()
5
import math
try:
a = eval(input('请输入底数:'))
b = eval(input('请输入真数:'))
c = math.log(b, a)
except ValueError:
if a<=0 and b>0:
print("底数不能小于等于0")
elif b<=0 and a>0:
print("真数不能小于等于0")
elif a<=0 and b<=0:
print('真数和低数都不能小于等于0')
except ZeroDivisionError:
print('底数不能为1')
except NameError:
print('输入必须为实数')
else:
print(c)
6
intxt = input("请输入明文:")
for p in intxt:
if "a" <= p <= "z":
print(chr(ord("a") + (ord(p) - ord("a") + 3)%26), end="")
elif "A" <= p <= "Z":
print(chr(ord("A") + (ord(p) - ord("A") + 3)%26), end="")
else:
print(p,end="")
第九套
1
while True:
s = input("请输入信息:")
if s=="Y" or s=="y":
break
2
import calendar
year = int(input("请输入年份:"))
table = calendar.calendar(year)
print(table)
3
s = input("请输入绕口令:")
print(s.replace("兵","将"))
4
5
fo = open("PY202.txt","w")
for i in range(1,10):
for j in range(1,i+1):
fo.write("{}*{}={}\t".format(j,i,i*j))
fo.write("\n")
fo.close()
6
fi = open("关山月.txt","r")
fo = open("关山月-诗歌.txt","w")
for i in fi.read():
if i == "。":
fo.write("。\n")
else:
fo.write(i)
fi.close()
fo.close()
fi = open("关山月-诗歌.txt","r")
fo = open("关山月-反转.txt","w")
txt = fi.readlines()
txt.reverse()
for line in txt:
fo.write(line)
fi.close()
fo.close()
第十套
1
data = eval(input("请输入一组数据,以逗号分隔:"))
print(max(data))
2
import jieba
s = "一件事情没有做过,就没有资格对此事发表看法"
ls = jieba.lcut(s)
print(ls)
3 这题不会
import time
t = time.localtime()
print(time.strftime("%Y年%m月%d日%H时%M分%S秒",t))
4 有点东西
for i in range(0, 4):
for y in range(0, 4 - i):
print(" ", end="")
print("* " * i)
for i in range(0, 4):
for x in range(0, i):
print(" ", end="")
print("* " * (4 - i))
5
from turtle import *
pensize(5)
for i in range(6):
fd(100)
right(60)
color("red")
circle(60,steps=6)
6
import random
letter_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z']
letter = letter_list[random.randint(0, 25)]
count = 0
while True:
a = input("请输入猜测的字母:")
count += 1
if a not in letter_list:
print("请重新输入字母:")
else:
if count >= 5:
print("你答错了5次,结束游戏!")
break
elif a > letter:
print("输入的字母在答案之后")
continue
elif a < letter:
print("输入的字母在答案之前")
continue
elif a == letter:
print("恭喜你答对了!共回答了{}次".format(count))
break
第十一套
1
f = open("poem.txt","r")
result = []
for line in f.readlines():
line = line.strip("\n")
if len(line) != 0 and line[0] != "#":
result.append(line)
result.sort()
for line in result:
print(line)
f.close()
2
a = []
for i in range(8):
a.append([])
for j in range(8):
a[i].append(0)
for i in range(8):
a[i][0] = 1
a[i][i] = 1
for i in range(2,8):
for j in range(1,i):
a[i][j] = a[i-1][j-1]+a[i-1][j]
for i in range(8):
for j in range(i+1):
print("{:3d}".format(a[i][j]),end=" ")
print()
3
def proc(strings):
m = 0
lst = []
for i in range(len(strings)):
if len(strings[i]) > m:
m = len(strings[i])
for i in range(len(strings)):
if len(strings[i]) == m:
lst.append(strings[i])
return lst
strings = ['cad' ,'VB', 'Python', 'MATLAB', 'hello', 'world']
result = proc(strings)
print("the longest words are:")
for item in result:
print("{: >25}".format(item))
4
strings = {'cad', 'PE ', 'Window', 'FM', 'hello', 'world','flowers'}
n=0
for word in strings:
if word.islower():
n += 1
print(n)
5
def proc(stu_list):
d = {}
for item in stu_list:
r = item.split("_")
a,b = r[0],r[1].strip()
if a in d:
d[a] += [b]
else:
d[a] = [b]
lst = sorted(d.items(), key = lambda d:len(d[1]), reverse = True)
return lst
f = open("signup.txt","r")
stu_list = f.readlines()
result = proc(stu_list)
for item in result:
print(item[0], '->', item[1])
f.close()
6
d = {"lili": 80, "xiaoqiang": 75, "yunyun": 89, "yuanyuan": 90, "wanghao": 85}
d_sort = sorted(d.items(), key=lambda x: x[1], reverse=True)
for i in range(3):
print(d_sort[i][0] + " " + str(d_sort[i][1]))
第十二套
1
num = input().split(",")
for i in num:
print("{:>10}".format(i),end='')
2
scale = 0.0001
def calv(base, day):
val = base * pow(1+scale,day*11)
return val
print('5年后的成就值是{}'.format(int(calv(1, 5*365))))
year = 1
while calv(1, year*365) < 100:
year += 1
print('{}年后成就值是100'.format(year))
3
while True:
try:
a = eval(input('请输入一个正整数: '))
if a > 0 and type(a)==int:
print(a)
break
else:
print("请输入正整数")
except:
print("请输入正整数")
4
import turtle as t
ls = [69, 292, 33, 131, 61, 254]
X_len = 400
Y_len = 300
x0 = -200
y0 = -100
t.penup()
t.goto(x0, y0)
t.pendown()
t.fd(X_len)
t.fd(-X_len)
t.seth(90)
t.fd(Y_len)
t.pencolor('red')
t.pensize(5)
for i in range(len(ls)):
t.penup()
t.goto(x0 + (i+1)*50, -100)
t.seth(90)
t.pendown()
t.fd(ls[i])
t.done()
5
import random
random.seed(2)
pdict = {'Alice': ['123456789'],
'Bob': ['234567891'],
'Lily': ['345678912'],
'Jane': ['456789123']}
name = input('请输入一个人名:')
plist = list(pdict)
if name in plist:
print(name, pdict.get(name)[0], random.randint(1000,9999))
else:
print("对不起,您输入的用户信息不存在。")
6
import jieba
def fenci(txt):
f = open(txt, "r")
datas = f.read()
f.close()
data = jieba.lcut(datas)
d = {}
for i in data:
if len(i) >= 2:
d[i] = d.get(i, 0) + 1
lt = list(d.items())
lt.sort(key=lambda x: x[1], reverse=True)
return lt
def show(lt):
for i in lt[:9]:
print(i[0], ":", i[1], end=',',sep="")
print(lt[9][0], ":", lt[9][1],sep="")
l1 = fenci("data2018.txt")
l2 = fenci("data2019.txt")
print(2019, end=':')
show(l2)
print(2018, end=':')
show(l1)
import jieba
def fenci(txt):
f = open(txt, "r")
datas = f.read()
f.close()
data = jieba.lcut(datas)
d = {}
for i in data:
if len(i) >= 2:
d[i] = d.get(i, 0) + 1
lt = list(d.items())
lt.sort(key=lambda x: x[1], reverse=True)
ls = [x[0] for x in lt[:10]]
return ls
def show(lt):
print(','.join(lt))
l1 = fenci("data2018.txt")
l2 = fenci("data2019.txt")
l3 = []
for i in l1:
if i in l2:
l3.append(i)
for i in l3:
l1.remove(i)
l2.remove(i)
print("共有词语:", end='')
show(l3)
print('2019特有:', end='')
show(l2)
print('2018特有:', end='')
show(l1)
第十三套
1
n = input('请输入一个正整数:')
for i in range(1,eval(n)+1):
print('{:0>2}{}'.format(i, '>'*i))
2
ns = input("请输入一串数据:")
dnum,dchr = 0,0
for i in ns:
if i.isnumeric():
dnum += 1
elif i.isalpha():
dchr += 1
else:
pass
print('数字个数:{},字母个数:{}'.format(dnum, dchr))
3
std = [['张三',90,87,95],['李四',83,80,87],['王五',73,57,55]]
modl = "亲爱的{}, 你的考试成绩是: 英语{}, 数学{}, Python语言{}, 总成绩{}.特此通知."
for st in std:
cnt = 0
for i in range(1,4):
cnt += st[i]
print(modl.format(st[0],st[1],st[2],st[3],cnt))
4
import turtle as t
import random as r
color = ['red','orange','blue','green','purple']
r.seed(1)
for i in range(5):
rad = r.randint(20,50)
x0 = r.randint(-100,100)
y0 = r.randint(-100,100)
t.color(r.choice(color))
t.penup()
t.goto(x0,y0)
t.pendown()
t.circle(rad)
t.done()
5
img = [0.244, 0.832, 0.903, 0.145, 0.26, 0.452]
filter = [0.1,0.8,0.1]
res = []
for i in range(len(img)-2):
k = 0
for j in range(len(filter)):
k += filter[j] * img[j+i]
print("k={:.3f} ,filter[{}]={:.3f} ,img[{}{}{}]={:.3f}".format(k,j,filter[j],i,'+',j,img[i+j]))
res.append(k)
for r in res:
print('{:<10.3f}'.format(r),end = '')
6
import jieba
fi = open("data.txt","r",encoding='utf-8')
data = fi.read()
fo = open("clean.txt","w")
s = ''
except_word = ",。?、‘’“”;:()\n--!"
for i in data:
if i not in except_word:
s += i
fo.write(s)
fi.close()
fo.close()
import jieba
fi = open("clean.txt", "r")
data = fi.read()
words = jieba.lcut(data)
d = {}
for word in words:
if len(word)>=3:
d[word] = d.get(word, 0) + 1
lt = list(d.items())
lt.sort(key=lambda x: x[1], reverse=True)
for i in lt[:9]:
print(i[0], ":", i[1], end=',', sep='')
print(lt[9][0], ":", lt[9][1], sep='')
fi.close()
第十四套
1
s = input()
print("{:\"^30x}".format(eval(s)))
2
n = input()
s = "〇一二三四五六七八九"
for c in "0123456789":
n = n.replace(c, s[eval(c)])
print(n)
3
a, b, c = [eval(x) for x in input().split(",")]
ls = []
for i in range(c):
ls.append(str(a*(b**i)))
print(",".join(ls))
4
import turtle
turtle.pensize(2)
for i in range(4):
turtle.fd(200)
turtle.left(90)
turtle.left(-45)
turtle.circle(100*pow(2,0.5))
5
while True:
s = input("请输入不带数字的文本:")
for i in s:
if i in '0123456789':
break
else:
break
print(len(s))
6
fi = open("data.txt", "r")
f = open("univ.txt", "w")
L = []
lines = fi.readlines()
for line in lines:
if 'alt=' in line:
begin = line.find('alt=')
end = line.find('"', begin + 5)
L.append(line[begin + 5:end])
for i in L:
f.write(i + "\n")
fi.close()
f.close()
f = open("univ.txt", "r")
n = 0
m = 0
L = []
names = f.readlines()
for name in names:
name = name.strip("\n")
if "大学生" not in name:
if "大学" in name:
L.append(name)
elif "学院" in name:
L.append(name)
for name in L:
if name[-2:] == "学院":
m += 1
else:
n += 1
print(name)
f.close()
print("包含大学的名称数量是{}".format(n))
print("包含学院的名称数量是{}".format(m))
第十五套
1
n = eval(input("请输入正整数:"))
print("{:=^14}".format(n))
2
a = [3, 6, 9]
b = eval(input())
j = 1
for i in range(len(a)):
b.insert(j, a[i])
j += 2
print(b)
3
import random
random.seed(100)
s = 0
for i in range(3):
a = random.randint(1, 9)
s += a ** 3
print(s)
4
import turtle
turtle.pensize(2)
d = 0
for i in range(1, 6):
turtle.fd(100)
d += 72
turtle.seth(d)
5
f = open("vote.txt")
names = f.readlines()
f.close()
n = 0
for name in names:
num = len(name.split())
if num == 1:
n += 1
print("有效票{}张".format(n))
f = open("vote.txt")
names = f.readlines()
f.close()
L = []
for name in names:
num = len(name.split())
if num == 1:
L.append(name.strip("\n"))
d = {}
for name in L:
d[name] = d.get(name,0) + 1
ls = list(d.items())
ls.sort(key=lambda x:x[1], reverse=True)
print("{}:{}".format(ls[0][0],ls[1][1]))
6
import jieba
fi = open("data.txt", "r")
f = open('out1.txt', 'w')
txt = fi.read()
words = jieba.lcut(txt)
words = list(set(words))
for word in words:
if len(word)>=3:
f.write(word+"\n")
fi.close()
f.close()
import jieba
fi = open("data.txt", 'r')
fo = open('out2.txt', 'w')
txt = fi.read()
words = jieba.lcut(txt)
d = {}
for word in words:
if len(word) >= 3:
d[word] = d.get(word, 0) + 1
ls = list(d.items())
ls.sort(key=lambda x: x[1], reverse=True)
for i in ls:
a = i[0]+":"+str(i[1])
fo.write(a+"\n")
fi.close()
fo.close()
第十六套
1
s = input("请输入一个正整数: ")
cs = 0
for c in s:
cs += eval(c)
print('{:=^25}'.format(cs))
2
s = input("请输入中文和字母的组合: ")
count = 0
for c in s:
if '\u4e00' <= c <= '\u9fff':
count += 1
print(count)
3
s = input("请输入一组数据: ")
ls = s.split(',')
lt = []
for i in ls:
lt.append(eval(i))
print(max(lt))
4
import turtle as t
import random as r
r.seed(1)
t.pensize(2)
t.pencolor('red')
angles = 6
points= [[0,0],[50,40],[70,80],[-40,30]]
for i in range(4):
x0,y0 = points[i]
t.penup()
t.goto(x0,y0)
t.pendown()
length = r.randint(6, 16)
for j in range(angles):
t.fd(length)
t.backward(length)
t.right(360 / angles)
t.done()
5
f = open('data.txt','r')
dic={}
for line in f:
l=line.strip().split(',')
if len(l)<3:
continue
dic[l[-1]]=dic.get(l[-1],[])+[l[1]]
unis=list(dic.items())
unis.sort(key=lambda x:len(x[1]),reverse=True)
for d in unis:
print('{:>4}: {:>4} : {}'.format(d[0],len(d[1]),' '.join(d[1])))
6
import jieba
f = "红楼梦.txt"
sf = "停用词.txt"
f1 = open(f, "r", encoding='utf-8')
datas = f1.read()
f1.close()
f2 = open(sf, "r", encoding='utf-8')
words = f2.read()
f2.close()
data = jieba.lcut(datas)
d = {}
word = ["一个", "如今", "一面", "众人", "说道", "只见", "不知",
"两个", "起来", "二人", "今日", "听见", "不敢", "不能",
"东西", "只得", "心中", "回来", "几个", "原来", "进来",
"出去", "一时", "银子", "起身", "答应", "回去"]
for i in data:
if len(i) < 2 or i in words or i in word:
continue
if i in ['凤姐', "凤姐儿", "凤丫头"]:
i = "凤姐"
elif i in ["宝玉", "二爷", "宝二爷"]:
i = "宝玉"
elif i in ["黛玉", "颦儿", "林妹妹", "黛玉道"]:
i = "黛玉"
elif i in ["宝钗", "宝丫头"]:
i = "宝钗"
elif i in ["贾母", "老祖宗"]:
i = "贾母"
elif i in ["袭人", "袭人道"]:
i = "袭人"
elif i in ["贾政", "贾政道"]:
i = "贾政"
elif i in ["贾琏", "琏二爷"]:
i = "贾琏"
d[i] = d.get(i, 0) + 1
l = list(d.items())
l.sort(key=lambda x: x[::-1], reverse=True)
f = open("result.csv", "w")
for i in l:
if i[1] < 40:
break
f.write(i[0] + ',' + str(i[1]) + '\n')
f.close()
第十七套
1
s = input("请输入一个小数: ")
s = s[::-1]
cs = 0
for c in s:
if c == '.':
break
cs += eval(c)
print('{:*>10}'.format(cs))
2
import time
t = input("请输入一个浮点数时间信息: ")
s = time.ctime(eval(t))
ls = s.split()
print(ls[3].split(':')[0])
3
import random
s = input("请输入随机种子: ")
ls = []
for i in range(26):
ls.append(chr(ord('a')+i))
for i in range(10):
ls.append(chr(ord('0')+i))
random.seed(eval(s))
for i in range(10):
for j in range(8):
print(ls[random.randint(0,35)],end='')
print()
4
import turtle as t
import random as r
r.seed(1)
t.pensize(2)
for i in range(3):
length = r.randint(20,80)
x0 = r.randint(-100, 100)
y0 = r.randint(-100, 100)
t.penup()
t.goto(x0,y0)
t.pd()
for j in range(4):
t.fd(length)
t.seth(90*(j+1))
t.done()
sumtime = 0
percls = []
ts = {}
with open('out.txt', 'r') as f:
for i in f:
i = i.strip().split(',')
ts[i[0]] = i[2]
sumtime += eval(i[1])
print('the total execute time is ', sumtime)
tns = list(ts.items())
tns.sort(key=lambda x: x[1], reverse=True)
for i in range(3):
print('the top {} percentage time is {}, spent in "{}" operation'.format(i, tns[i][1], tns[i][0]))
6
f = open("八十天环游地球.txt")
fo = open("八十天环游地球-章节.txt","w")
txt = f.readlines()
for line in txt:
line = line.strip()
if line[0]=="第" and line[2]=="章":
fo.write(line+"\n")
fo.close()
f.close()
import jieba
f = open("八十天环游地球.txt")
datas = f.readlines()
l = []
for i in range(len(datas)):
if datas[i][0] == "第" and datas[i][2] == "章":
l.append(i)
for i in range(len(l)):
if i != len(l) - 1:
data = ''.join(datas[l[i]:l[i + 1]])
else:
data = ''.join(datas[l[i]:])
s = data.split()[0]
words = jieba.lcut(data)
d = {}
for y in words:
if len(y) < 2:
continue
d[y] = d.get(y, 0) + 1
lis = list(d.items())
lis.sort(key=lambda x: x[1], reverse=True)
print(s, lis[0][0], lis[0][1])
f.close()
新增1
1
s=input()
print("{:=>25,}".format(eval(s)))
2
s=input()
print(s[::-1])
print(len(s))
3
a, b, c = [int(x) for x in input().split(',')]
ls = []
for i in range(c):
ls.append(a + (i * b))
print(ls)
4
import turtle
turtle.pensize(2)
d=0
for i in range(1, 13):
turtle.fd(40)
d += 30
turtle.seth(d)
5
ls = [111, 222, 333, 444, 555, 666, 777, 888, 999]
lt = [999, 777, 555, 333, 111, 888, 666, 444, 222]
s = 0
for i in range(len(ls)):
s += ls[i]*lt[i]
print(s)
6
L = []
fo = open("score.txt", "r")
fi = open("candidate0.txt", "w")
lines = fo.readlines()
for line in lines:
line = line.strip()
student = line.split(' ')
sum = 0
for i in range(1, 11):
sum += int(student[-i])
student.append(str(sum))
L.append(student)
L.sort(key=lambda x: x[-1], reverse=True)
for i in range(10):
fi.write(' '.join(L[i][:-1]) + '\n')
fo.close()
fi.close()
fi = open("candidate0.txt", "r")
fo = open("candidate.txt", 'w')
L = []
lines = fi.readlines()
for line in lines:
line = line.strip()
student = line.split(' ')
for i in student[2:]:
if int(i) < 60:
break
else:
L.append(student[:2])
for i in L:
fo.write(' '.join(i) + '\n')
fi.close()
fo.close()
新增2
1
n = eval(input("请输入正整数:"))
print("{:*>15}".format(n))
2
a = [3,6,9]
b = eval(input())
c = []
for i in range(3):
c.append(a[i]+b[i])
print(c)
3
import random
random.seed(0)
s = 0
for i in range(5):
n = random.randint(1,97)
s = s + n**2
print(s)
4
import turtle
turtle.pensize(2)
d = 0
for i in range(1, 9):
turtle.fd(100)
d += 45
turtle.seth(d)
5
f=open("name.txt")
names=f.readlines()
f.close()
f=open("vote.txt")
votes=f.readlines()
f.close()
f=open("vote1.txt","w")
D={}
NUM=0
for vote in votes:
num = len(vote.split())
if num==1 and vote in names:
D[vote[:-1]]=D.get(vote[:-1], 0)+1
NUM+=1
else:
f.write(vote)
f.close()
l=list(D.items())
l.sort(key=lambda s:s[1],reverse=True)
name=l[0][0]
score=l[0][1]
print("有效票数为:{} 当选村长村民为:{},票数为:{}".format(NUM,name,score))
6
import jieba
f = open('data.txt','r')
lines = f.readlines()
f.close()
f = open('out.txt','w')
for line in lines:
line=line.strip(' ')
wordList = jieba.lcut(line)
f.writelines('\n'.join(wordList))
f.close()
import jieba
f = open('out.txt','r')
words = f.readlines()
f.close()
D={}
for w in words:
D[w[:-1]]=D.get(w[:-1], 0) + 1
print("曹操出现次数为:{} ".format(D["曹操"]))
新增3
1
ls = eval(input())
s = ""
for item in ls:
if type(item) == type("香山"):
s += item
print(s)
2
import random
random.seed(25)
n = random.randint(1,100)
for m in range(1,7):
x = eval(input("请输入猜测数字:"))
if x == n:
print("恭喜你,猜对了!")
break
elif x>n:
print("大了,再试试")
else:
print("小了,再试试")
if m==6:
print("谢谢!请休息后再猜")
3
def f(n):
s = 0
if n%2==1:
for i in range(1, n+1, 2):
s += 1/i
else:
for i in range(2, n+1, 2):
s += 1/i
return s
n = int(input())
print('{:.2f}'.format(f(n)))
4
import turtle as t
color = ['red','green','blue']
rs = [10,30,60]
for i in range(3):
t.penup()
t.goto(0, -rs[i])
t.pd()
t.pencolor(color[i])
t.circle(rs[i])
t.done()
5
import jieba
s = input("请输入一段中文文本,句子之间以逗号或句号分隔:")
slist = jieba.lcut(s)
m = 0
for i in slist:
if i in ",。":
continue
m += 1
print(i, end='/')
print("\n中文词语数是:{}\n".format(m))
ss = ''
for i in s:
if i in ',。':
print('{:^20}'.format(ss))
ss = ''
continue
ss += i
6
fi = open("data.txt",'r')
fo = open("studs.txt",'w')
datas = fi.readlines()
for data in datas:
data1 = data.strip().split(':')
data2 = data1[1].split(',')
name = data1[0]
score = data2[1]
fo.write(name+":"+score+"\n")
fi.close()
fo.close()
fi = open("data.txt",'r')
datas = fi.readlines()
d = {}
for data in datas:
data1 = data.strip().split(':')
data2 = data1[1].split(',')
name = data1[0]
score = data2[1]
d[name] = score
lst = list(d.items())
lst.sort(key=lambda x:x[1], reverse=True)
print(lst[0][0]+":"+lst[0][1])
fi.close()
fi = open("data.txt", 'r')
datas = fi.readlines()
d = {}
for data in datas:
data1 = data.strip().split(':')[1]
banji, score = data1.split(',')
d[banji] = d.get(banji, []) + [int(score)]
for i in d.items():
print(i[0] + ":" + "{:.2f}".format(sum(i[1]) / len(i[1])))
fi.close()
|