python改进婚礼礼金请帖

复制代码
# 改进婚礼礼金程序
# 。用“w”模式,打开一个文件“婚礼礼金.txt”
# 。写一个while ture
# 。请输入”姓名,礼金“,例如”小明 1000“
# 。将数据存储入文件中,每行一个数据
# 。输入quit 退出循环
# 。退出循环重新读取文件,按行读取
# 把礼金读取到list中
# 输出list的统计数据,加和、最高、最低、平均值
python 复制代码
fname = '婚礼礼金.txt'#礼金名单名字赋值给fname
with open(fname,'w',encoding="utf8")as f:

    while True:#如果真
        print("#" * 20)#输出20个#
        info = input("请输入来人和金额:")#将输入的人名和金额赋值给info
        if info == "quit":#如果该值和quit相等
            break#退出
        fields = info.split()#######将该串用空白分隔符分隔开来
        if len(fields) == 2:
            name, money = fields#如果该串分割后值为2将分割后的第一个第二个分别赋值为名字和钱
        else:
            continue#否则退出
        money = int(money)#把money转化为int类型
        f.write(f"{name},{money}\n")
with open(fname,encoding="utf8")as f:
    moneys=[]
    for line in f:
        line=line[:-1]
        fields = line.split(",")
        if len(fields) == 2:
            name, money = fields
        else:
            continue  # 否则退出
        moneys.append(int(money))
    print("加和:",sum(moneys))
    print("最高:", max(moneys))
    print("最低:", min(moneys))
    print("平均:", sum(moneys)/len(moneys))
相关推荐
xiangjiaodalishi11 分钟前
武汉玖叁鹿科技:我们把 GEO 服务做成了可以审计的交付工程
python·1024程序员节
zzu123zsw27 分钟前
为什么 Python 做 Agent 总在并发中崩溃?拆解 Blockcell 在 Rust 下的 Host-Cell 隔离与自愈状态机
开发语言·python·rust·大模型·ai agent
jiangliushi630 分钟前
GPT-6.1 Sol 免费用:Python 读文件报 ‘gbk‘ codec can‘t decode?加一个参数就好
python·编码·1024程序员节
餐赢计餐饮策划设计1 小时前
如何判断国潮养生设计是否适合自己?
python·1024程序员节
艾灸爱好者1 小时前
艾草精油专业制造商哪3点决定品质高低?
python·1024程序员节
weixin_440401691 小时前
大数据技术-人工智能编程题
大数据·人工智能·python·1024程序员节
“AI国潮设计-小江”2 小时前
AIGC实战:Python+SDXL批量生成英歌舞IP立牌,解锁猪猪蛋糕“反差萌”流量密码(附完整API源码)
开发语言·人工智能·python·prompt·aigc
成信吴彦祖2 小时前
当前专业的无人机RID侦测主站供应商主要有哪些
python·1024程序员节
编程的一拳超人2 小时前
【元宝搜索开发者征文大赛】四语实战:Java/Python/Go/Node.js 构建 AI 搜索应用全攻略
java·python·golang·node.js·agent
fanruitian3 小时前
python anaconda jupyter notebook
python