第1关 股票交易金额
f1=open("test/股票信息.csv","r",encoding="utf8")
gpzd={}
x=input("股票代码")
y=eval(input("股数"))
#代码开始
for line in f1:
parts = line.strip().split(',')
gpzd[parts[0]] = float(parts[2])
if x in gpzd:
amount = gpzd[x] * y
commission = round(amount * 0.003, 2)
print(f"金额{amount:.2f}佣金{commission:.2f}")
else:
print("代码错误")
#代码结束
f1.close()
第2关 超市销售计算
txm=input("条形码")
sl=eval(input("数量"))
f1=open("test/超市商品.csv","r",encoding="utf8")
#代码开始
spzd = {}
next(f1)
for line in f1:
parts = line.strip().split(',')
spzd[parts[0]] = float(parts[5])
if txm in spzd:
total_amount = round(spzd[txm] * sl, 2)
print(f"金额{total_amount:.2f}")
else:
print("条形码错误")
f1.close()
#代码结束
第3关 银行种类统计
f1=open("test/银行信息.csv","r",encoding="utf8")
#代码开始
category_count = {}
next(f1)
for line in f1:
parts = line.strip().split(',')
category = parts[-1]
if category in category_count:
category_count[category] += 1
else:
category_count[category] = 1
f1.close()
yhlb = sorted(category_count.items(), key=lambda item: item[1],reverse=True)
#代码结束
n=eval(input())
for x in yhlb[:n]:
print(x[0],x[1])
第4关 国债收益计算
import csv
gzbm=input("国债编号")
je=eval(input("金额"))
f1=open("test/国债信息.csv","r",encoding="utf8")
#代码开始
bond_rates = {}
csv_reader = csv.reader(f1)
next(csv_reader)
for row in csv_reader:
bond_id = row[0]
rate = float(row[3]) / 100
bond_rates[bond_id] = rate
f1.close()
if gzbm in bond_rates:
income = je * bond_rates[gzbm]
print(f"年收益{income:.2f}")
else:
print("国债编号错误")
#代码结束
第5关 十二钗的出场次数
f1=open("test/红楼梦.txt","r",encoding='utf-8')
xm=["黛玉","宝钗","元春","探春","湘云","妙玉","迎春","惜春","凤姐","巧姐","李纨","可卿"]
#代码开始
content=f1.read()
count_dict = {}
for name in xm:
count_dict[name] = content.count(name)
sorted_count = sorted(count_dict.items(), key=lambda x: x[1], reverse=True)
for name, count in sorted_count[:5]:
print(name, count)
#代码结束
f1.close()
第6关 计算蛋白质质量
dbz=input("输入蛋白质字符串")
f1=open("test/dbzzl.txt")
#代码开始
if not dbz.isalpha() or not dbz.isupper():
print("输入错误")
else:
mass_dict = {}
for line in f1:
parts = line.split()
if len(parts) == 2:
mass_dict[parts[0]] = float(parts[1])
try:
total_mass = 0.0
for protein in dbz:
total_mass += mass_dict[protein]
print(f"质量{total_mass:.3f}")
except KeyError:
print("输入错误")
f1.close()
#代码结束
第7关 班级成绩统计
xsxx=[]
def srsj():
sj=input("")
while(len(sj)>0):
x=sj.split(",")
xsxx.append([x[0],x[1],eval(x[2]),eval(x[3]),eval(x[4])])
sj=input("")
srsj()
#代码开始
bjcj={}
for info in xsxx:
average=sum(info[2:])/3
bj=info[0]
bjcj[bj]=bjcj.get(bj,0)+average
#代码结束
for x in bjcj:
print("班级{}\t{:.2f}".format(x,bjcj[x]))
第8关 出现次数最多的数
#代码开始
# 初始化一个空字典来存储每个数字及其出现的次数
count_dict = {}
# 读取输入直到0出现
while True:
num = int(input())
if num == 0:
break
if num in count_dict:
count_dict[num] += 1
else:
count_dict[num] = 1
# 找出出现次数最多的数字。如果次数相同,则选择最小的数字
most_frequent = None
max_count = 0
for num in count_dict:
if count_dict[num] > max_count or (count_dict[num] == max_count and num < most_frequent):
most_frequent = num
max_count = count_dict[num]
# 输出出现次数最多的数字
print(most_frequent)
#代码结束