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))
相关推荐
在放️10 小时前
Python 数据分析 · 进入自动化处理的关键入门信息
python
袁袁袁袁满11 小时前
Haystack与亮数据MCP工具结合实现自动化爬虫
爬虫·python·网络爬虫·数据采集·爬虫实战·视频爬虫·特推爬虫
newbiai11 小时前
2026马年春晚:火山引擎驱动AI新体验?
人工智能·python·火山引擎
小鸡吃米…11 小时前
TensorFlow 实现异或(XOR)运算
人工智能·python·tensorflow·neo4j
深蓝电商API12 小时前
Redis 作为爬虫去重与任务队列实战
爬虫·python
郝学胜-神的一滴12 小时前
FastAPI:Python 高性能 Web 框架的优雅之选
开发语言·前端·数据结构·python·算法·fastapi
柒.梧.12 小时前
Java位运算详解:原理、用法及实战场景(面试重点)
开发语言·数据库·python
Scott.W12 小时前
跟我学Easyi3C Tower Adapter Console(9)
人工智能·python·嵌入式硬件·i3c
多恩Stone12 小时前
【3D-AICG 系列-14】Trellis 2 的 Texturing Pipeline 保留单层薄壳,而 Textured GLB 会变成双层
人工智能·python·算法·3d·aigc
刘恒12345678912 小时前
Windows 电脑文件夹手动分类指南
java·windows·python·电脑·php