我一直在用python开发员工计划,我面临着困难
输出应该如下所示
from array import *
arr = array("i", [])
n= (int(input("Enter the number of employee's: ")))
arr.append(n)
for i in range(1,n+1):
name = input("Enter the name of the employee %s: " %(i))
from array import *
salary = array("f", [])
x = int(input("Enter %s current salary: " %(name)))
arr.append(x)
from array import *
Q1 = array("f", [])
rating1 = int(input("Enter the rating %s received for Q1: " %(name)))
arr.append(rating1)
from array import *
Q2 = array("f", [])
rating2= int(input("Enter the rating %s received for Q2: " %(name)))
arr.append(rating2)
from array import *
Q3 = array("f", [])
rating3 = int(input("Enter the rating %s received for Q3: " %(name)))
arr.append(rating3)
from array import *
Q4 = array("f", [])
rating4 = int(input("Enter the rating %s received for Q4: " %(name)))
arr.append(rating4)
def totalrating():
totalrating = [rating1, rating2, rating3, rating4]
total = sum(totalrating)
return(total)
def overallrating():
overallrating = totalrating()/4
return(overallrating)
def display():
print(name, x, rating1, rating2, rating3, rating4, totalrating(), overallrating())
display()
我的程序就像
输出结果仅返回第二个值,而不是第一个,您能帮我做什么?
答案 0 :(得分:0)
好的,所以这里有很多清理工作。我将指出代码注释中的更改,并根据需要尝试进一步解释。
# The multiple import of array are redundant and the way you are using this is actually
# wrong so it's not needed. So I am commenting out this one and then deleting the rest.
# from array import *
# Here you just need a basic list and not an instance of the array module.
# arr = array("i", []) <- old code for reference.
arr = []
n= (int(input("Enter the number of employee's: ")))
# I commented out this line because you don't ever access this element after you set it
# so you can just not set it at all.
# arr.append(n)
# Because we got rid of the append above we can loop from 0 to n instead of 1 to n+1
for i in range(0, n):
name = input("Enter the name of the employee %s: " %(i))
# These sub arrays is unnecessary and part of the problem
# salary = array("f", [])
salary = int(input("Enter %s current salary: " %(name)))
# So the way you are appending stuff is part of the problem.
# Let's remove all of these appends out and we can do them at the end of the loop
# arr.append(x)
rating1 = int(input("Enter the rating %s received for Q1: " %(name)))
rating2 = int(input("Enter the rating %s received for Q2: " %(name)))
rating3 = int(input("Enter the rating %s received for Q3: " %(name)))
rating4 = int(input("Enter the rating %s received for Q4: " %(name)))
# Here we can append a dictionary to the array with the data laid out how you want it.
arr.append({
'name': name,
'salary': salary,
'rating1': rating1,
'rating2': rating2,
'rating3': rating3,
'rating4': rating4,
})
# Note the display function is now passing in the employee info here.
def totalrating(employee):
# So this function can be modified a few ways
# Also don't name variables the same as functions. It can lead to name clashing issues.
'''
# This is the original code and I am leaving is for posterity.
totalrating = [rating1, rating2, rating3, rating4]
total = sum(totalrating)
return(total)
'''
'''
# Here is the first way it could be modified.
total_rating = [
employee['rating1'], employee['rating2'], employee['rating3'],
employee['rating4']
]
total = sum(total_rating)
return total
# not there is no need to put () around the return in python
'''
# Here is how I would modify it though.
rating_keys = ['rating1', 'rating2', 'rating3', 'rating4']
return sum([employee[rate] for rate in rating_keys])
# This can be rewritten in one line and since it's only used in one place it should be done there
# def overallrating():
# overallrating = totalrating()/4
# return(overallrating)
def display():
# Now that the array has been cleaned up we can go through the array and print it.
for employee in arr:
# Instead of calculating them in the print statement just make variables ahead of time
total = totalrating(employee)
# This whole function is a one liner that should just be done here.
overall_rating = total/4
print(
employee['name'], employee['salary'], employee['rating1'], employee['rating2'],
employee['rating3'], employee['rating4'], total, overall_rating
)
display()
这应该按照您的要求输出。清除后不带注释和旧代码,最终产品是:
arr = []
n= (int(input("Enter the number of employee's: ")))
for i in range(0, n):
name = input("Enter the name of the employee %s: " %(i))
salary = int(input("Enter %s current salary: " %(name)))
rating1 = int(input("Enter the rating %s received for Q1: " %(name)))
rating2 = int(input("Enter the rating %s received for Q2: " %(name)))
rating3 = int(input("Enter the rating %s received for Q3: " %(name)))
rating4 = int(input("Enter the rating %s received for Q4: " %(name)))
arr.append({
'name': name,
'salary': salary,
'rating1': rating1,
'rating2': rating2,
'rating3': rating3,
'rating4': rating4,
})
def totalrating(employee):
rating_keys = ['rating1', 'rating2', 'rating3', 'rating4']
return sum([employee[rate] for rate in rating_keys])
def display():
for employee in arr:
total = totalrating(employee)
overall_rating = total/4
print(
employee['name'], employee['salary'], employee['rating1'], employee['rating2'],
employee['rating3'], employee['rating4'], total, overall_rating
)
display()