第1关 统计文档中作者作品数量
f1=open("sy9//中学诗词.txt","r",encoding='utf-8')
#代码开始
zj={}
for x in f1:
if " " in x and not "," in x and not "!" in x :
x=x.strip('\n').strip(' ')
y=x.rfind(' ')
name=x[y+1:]
zj[name]=zj.get(name,0)+1
lt=list(zj.items())
lt.sort(key=lambda x:x[1],reverse=True)
#代码结束
for i in lt:
print(i[0],i[1])
f1.close()
第2关 候选人票数统计
tp=[]
x=input("")
while(x!="end"):
tp.append(x)
x=input("")
#代码开始
zd={}
for y in tp:
zd[y]=zd.get(y,0)+1
items=list(zd.items())
items.sort(key=lambda h:h[1],reverse=True)
#代码结束
for i in range(len(items)):
print("第{}名姓名{}票数{}".format(i+1,items[i][0],items[i][1]))
第3关 会员会费计算
f1=open("sy9//会员名单.txt","r",encoding="utf8")
#代码开始
dict1={"影视黄金会员":199,"影视星钻会员":399,"非影视会员":0}
dict2={"体育大众会员":98,"体育专业会员":198,"非体育会员":0}
for x in f1:
s=x.strip().split(",")
print(s[0],dict1[s[1]]+dict2[s[2]])
#代码结束
f1.close()
第4关 职工工资计算
zg=[]
xx=input("")
while(xx!="end"):
sj=xx.split(',')
zg.append([sj[0],eval(sj[1]),sj[2],sj[3]])
xx=input("")
#代码开始
yfgz=[]
zd1={"北京":5000,"上海":4000,"广州":3000}
zd2={"销售部":2000,"经理室":3000,"财会部":1000}
for yg in zg:
gz=yg[1]+zd1[yg[2]]+zd2[yg[3]]
yfgz.append([yg[0],gz])
yfgz.sort(key=lambda y:y[1],reverse=True)
#代码结束
for x in yfgz:
print("姓名"+x[0]+"工资"+str(x[1]))
第5关 文件币种兑换计算
f1=open("sy9//汇率兑换.txt","r",encoding="utf-8")
bzzd={}
#代码开始
for x in f1:
bz=x[x.find("(")+1:x.find("(")+4]
hv=x[x.find("=")+1:x.find("人民币")]
bzzd[bz]=eval(hv)
hb=input("")
while hb!="0":
bz=hb[:3].upper()
if bz not in bzzd:
print("币种错误")
else:
if not hb[3:].isnumeric():
print("数字错误")
else:
je=eval(hb[3:])
zhje=bzzd[bz]*je
print("人民币{:.2f}".format(zhje))
hb=input("")
#代码结束
f1.close()
第6关 饮品销售计算
f1=open("sy9//nc.csv","r",encoding="utf8")
#代码开始
nczd={}
for line in f1:
x=line.strip("\n").split(",")
nczd[x[0]]=eval(x[2])
print("编号{}饮品{}价格{}".format(x[0],x[1],x[2]))
yp=input("请选择饮品")
je=0
while yp!="00":
sl=eval(input("请输入数量"))
je=je+sl*nczd[yp]
yp=input("请选择饮品")
if yp=="99":
print("编码错误")
yp=input("请选择饮品")
sl=eval(input("请输入数量"))
je=je+sl*nczd[yp]
yp=input("请选择饮品")
break
#代码结束
print("应付{}元".format(je))
第7关 统计文件词语的词频
import jieba
f1= open("sy9//宋词.txt", "r")
#代码开始
txt=f1.read()
pc=set()
f1.seek(0,0)
for line in f1:
if " " in line :
cp=line[:line.find(" ")]
pc.add(cp)
xm=line[line.find(" ")+1:].strip()
pc.add(xm)
f1.close()
for x in pc:
txt=txt.replace(x,"")
words = jieba.lcut(txt)
counts = {}
for word in words:
if len(word)>1 :
counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
#代码结束
for i in range(15):
print("{}{}".format(items[i][0],items[i][1]))
第8关 宋词文件词云图片
import jieba
import wordcloud
f1= open("sy9//宋词.txt", "r")
#代码开始
txt=f1.read()
pc=set()
f1.seek(0,0)
for line in f1:
if " " in line:
cp=line[:line.find(" ")]
pc.add(cp)
xm=line[line.find(" ")+1:].strip("\n")
pc.add(xm)
f1.close()
for x in pc:
txt=txt.replace(x,"")
counts = {}
words = jieba.lcut(txt)
for word in words:
if len(word)>1 :
counts[word] = counts.get(word,0) + 1
w=wordcloud.WordCloud(font_path="sy9//simhei.ttf",background_color='white',max_words=300,width=1000,height=700)
w.generate_from_frequencies(counts)
#代码结束
w.to_file("sy9//pict//sc1.png")