python - 如何在文件处理中找到平均值的总和
问题描述
所以..我需要这个项目的一些帮助。我开始学习一些关于 python 的知识,我们的讨论是关于文件处理的。我做了正确的执行,但这里的问题是我需要找到给定循环的总和和平均值。
def num3():
ifile = open(r'sales.txt','w')
no_days = 7
for count in range(1, no_days+1):
print('List of Sales in Php')
sales = float(input('Day' +str(count)+ ':\tPhp'))
ifile.write(str(sales)+'\n')
ifile.close()
num3()
解决方案
这应该有效:
def num3():
with open('sales.txt','w') as fp:
no_days = 7
print('List of Sales in Php')
total = 0
for count in range(1, no_days+1):
sales = float(input('Day' +str(count)+ ':'))
total += sales
total = total
average = total/no_days
fp.write(f'Total Value: {total}\nAverage Value: {average}')
num3()
推荐阅读
- ruby-on-rails - 使用 RSpec 测试 Rails CRUD 方法
- c++ - For循环没有按公式输出正确的结果
- c# - 在没有单独的 id 属性的情况下更新实体框架中的外键?
- java - JPMS 如何知道哪些包属于哪些模块?
- asp.net-mvc - 我应该怎么做才能避免不同的日期格式,如果它适用于在所有具有不同日期格式的浏览器中使用 dd/mm/yy
- node.js - Node.js,在私有仓库上创建一个 github 问题
- html - 让 CSS 选项卡默认打开
- python - 将图像编辑为 tensorflow 张量 python
- android-studio - 向现有 Android Studio 项目添加单元测试
- php - 使用表单和 PDO 插入多行