RoyM
RoyM

Reputation: 37

Calculate the sum of the salary of a particular staff

I am a newbie in python and currently working on an assignment question and it required me to calculate the staff's salary based on the list salary_records

if the user input Billy, it will output the sum of Billy's twelve-month salary. if the input is not the name of the staff, it will output [name] not found.

My problem is that I can't print [name] not found and would like to ask for some help, thanks so much! Here is what I have got now.

salary_records = ['Billy 12300 11700 11100 10300 10400 14800 14900 13600 12300 14600 13500 14900\n', 
              'Betty 11900 11800 15000 13000 12500 14000 11500 11100 12400 10900 20000 10300\n', 
              'Apple 13600 13700 10900 11900 12000 14900 13600 12400 11700 13700 10300 13900\n', 
              'Kelly 11400 11600 14400 10800 12700 14900 13300 12700 11900 13800 11800 13500\n', 
              'Gigi 14400 12400 11600 11600 12800 13600 11500 14300 13200 10200 14400 14400\n']

a=[]

n=input()

for i in salary_records:
    c = i.split( )

    if c[0] == n:
        a.append(c[1:13])
        c.sort(key=lambda x: x)
        del c[-1]
        c = list(map(int, c))
        print(n+' earns ' + str(sum(c)))

Upvotes: 1

Views: 576

Answers (2)

Lev Leontev
Lev Leontev

Reputation: 2615

You can a flag to check that the staff exists:

salary_records = ['Billy 12300 11700 11100 10300 10400 14800 14900 13600 12300 14600 13500 14900\n', 
          'Betty 11900 11800 15000 13000 12500 14000 11500 11100 12400 10900 20000 10300\n', 
          'Apple 13600 13700 10900 11900 12000 14900 13600 12400 11700 13700 10300 13900\n', 
          'Kelly 11400 11600 14400 10800 12700 14900 13300 12700 11900 13800 11800 13500\n', 
          'Gigi 14400 12400 11600 11600 12800 13600 11500 14300 13200 10200 14400 14400\n']

a=[]

n=input()
found_name = False
for i in salary_records:
    c = i.split( )

    if c[0] == n:
        found_name = True
        a.append(c[1:13])
        c.sort(key=lambda x: x)
        del c[-1]
        c = list(map(int, c))
        print(n+' earns ' + str(sum(c)))
if not found_name:
    print('%s not found' % n)

Also, to separate the salaries from the name, you can just delete the first element:

if c[0] == n:
    found_name = True
    del c[0]
    c = list(map(int, c))
    print(n + ' earns ' + str(sum(c)))

Upvotes: 2

Patrick Haugh
Patrick Haugh

Reputation: 61032

We need to convert your list of strings into a dictionary mapping names to the sums of their salaries. We'll strip the surrounding whitespace, then split each string into words. Then we will sum the numeric values together and form the mapping

salary_records = map(str.strip, salary_records)
salary_records = map(str.split, salary_records)
salary_records = {name: sum(map(int, months)) for name, *months in salary_records}

def get_salary(name):
    if name in salary_records:
        return salary_records[name]
    else:
        raise KeyError("Name {} not found".format(name))

get_salary('Billy')
# 154400

Upvotes: 6

Related Questions