【发布时间】:2021-07-02 12:47:54
【问题描述】:
我只能使用一个循环,一个if 语句,并且我使用我的唯一 if 语句来过滤 csv 文件中的行以打印具有相同作业名称的特定行。所以我用tryexcept 来求平均工资收入。
为了在for 循环中给出值,我使用sum() 来填充total_salaries,但是因为薪水是浮动的,所以它不起作用。这是代码:
for line in reader:
name, job, salary = line
salary = float(salary)
salary = salary + (salary*0.1)
instructor_count = len(line)
total_salaries = sum(salary)
我尝试写 total_salaries = sum(float, Salary) 但这给了我一个类型错误
这是获取完整图片的完整代码:
import csv
file = open('SP21-Emp1.txt', 'r')
reader = csv.reader(file)
total_salaries = 0
instructor_count = 0
print("Reading from file SP21-Emp1.txt" "\n")
for line in reader:
name, job, salary = line
salary = float(salary)
salary = salary + (salary*0.1)
instructor_count += 1
total_salaries += salary
if job == 'Instructor':
instructor_count += 1
total_salaries += salary
first_name, last_name = name.split(' ', 1)
print(f'Name: {last_name} {first_name} \t Job: {job} \t Income: {salary}',)
try:
average_instructor_salary = total_salaries / instructor_count
except ValueError:
print('can not calculate average because there were no instructor salaries found.')
average_instructor_salary = "No instructor salary found"
my output was this:
Reading from file SP21-Emp1.txt
Name: Tweijari Bilal Job: Instructor Income: 2200.0
Name: Hachem Nizar Job: Instructor Income: 1980.0
Name: Saeed Walaa Job: Instructor Income: 2090.0
Name: Khattar Nadine Job: Instructor Income: 1628.0
The average of the instructors' salaries is: 1493.8
but if you look at the average for my output and the expected output that is coming up you will see that it is different.
Reading from file SP21-Emp1.txt
Name: Tweijari, Bilal Job: Instructor Income: $2200.0
Name: Hachem, Nizar Job: Instructor Income: $1980.0
Name: Saeed, Walaa Job: Instructor Income: $2090.0
Name: Khattar, Nadine Job: Instructor Income: $1628.0
The average of the instructors' salaries is: 1974.5
this is the latest update, i am a certified dumbass, i copyed and pasted the print line in the wrong place... so now i do get a value but it is still not the expected value
import csv
file = open('SP21-Emp1.txt', 'r')
reader = csv.reader(file)
total_salaries = 0
instructor_count = 0
print("Reading from file SP21-Emp1.txt" "\n")
for line in reader:
name, job, salary = line
salary = float(salary)
salary = salary + (salary*0.1)
instructor_count += 1
total_salaries += salary
if job == 'Instructor':
first_name, last_name = name.split(' ', 1)
print(f'Name: {last_name} {first_name} \t Job: {job} \t Income: {salary}',)
try:
average_instructor_salary = total_salaries / instructor_count
print("\n" "The average of the instructors' salaries is:", average_instructor_salary)
except ValueError:
print('can not calculate average because there were no instructor salaries found.')
average_instructor_salary = "No instructor salary found"
【问题讨论】:
-
salary只是一项,所以sum(salary)只是要返回工资。你想要的是total_salaries += salary。 -
第一次很棒,我真的得到了一个价值,我爱死你了……但答案与预期的输出不同。我将编辑我的帖子并显示结果。
-
同样的事情适用于
instructor_count = len(line)。你只想要instructor_count += 1。 -
该值现在非常接近,但仍与预期值不同,我的输出为 1493.8,预期输出为 1974.5。所以我的整个方程式一直都是错的吗?
-
我剪切并粘贴了您的确切代码,得到 1974.5。你是如何打印最后一行的?