2013-04-10 63 views
-1

我想显示最低工资(我已经计算出)以及与最低工资相关的名称。它会是这个样子它打印时:将最低工资为:$ 4500,制作人:约翰Python:如何显示最低工资名称

amount = int(input("How many employees?: ")) 
if amount <= 0: 
    print("You cannot have 0 or less.") 
name = [] 
salary = [] 
length = len(salary) 
mini = 200000 
maxi = 0 
combined = (name, salary) 

for i in range(1, amount + 1): 
    employee = input("What is the employee's name?: ") 
    name += [employee] 
    earned = int(input("How much is the salary? It cannot be less than 0 or over $200,000: ")) 
    while earned <= 0 or earned >= 200000: 
     earned = int(input("How much is the salary? It cannot be less than 0 or over $200,000: ")) 

     mini = earned 
     maxi = earned 
    salary += [earned] 
    if earned < mini: 
     mini = earned 
    if earned > maxi: 
     maxi = earned 

average = sum(salary)/len(salary) 

print('The Average Salary is: $',average) 
print('The Lowest Salary is: $',mini,'Produced by: ',name) 
print('The Highest Salary is: $',maxi,'Produced by: ',name) 

回答

0
print('The Lowest Salary is: ' + str(mini) + ' Produced by: ' + name) 

你也可以做这样的事情:

print('The Lowest salary is %d Produced by: %s' % (mini, name)) 
0

考虑到你有名单像这样的:

In [153]: names=["foo","bar","spam","eggs"] 

In [154]: salary=[100,150,50,170] 

#highest salary 
In [155]: _,name=max(enumerate(names),key=lambda x:salary[x[0]]) 

In [156]: name 
Out[156]: 'eggs' 

#Lowest salary 
In [157]: _,name=min(enumerate(names),key=lambda x:salary[x[0]]) 

In [158]: name 
Out[158]: 'spam' 
2

,或者你可以只找到最低的指数和最大

maxi = salary.index(max(salary)) 
mini = salary.index(min(salary)) 

maxsal = salary[maxi] 
maxname = name[maxi] 

minsal = salary[mini] 
minname = name[mini] 
+0

这是'O(N^2)'的时间复杂度。 – 2013-04-10 18:11:32