简体   繁体   中英

Extract value from dictionary - modifying it

I've been stuck on this question for school for longer than I care to admit...I've done about 5 different iterations of how to tackle this, my latest being below the question. This is for a class I'm taking.

given the following dictionary of employees and salaries, create an personalized salary message letting each employee know they have been given a 2% raise and the new total of their salary.

expected outcome:

 John, your current salary is 54000.00. You received a 2% raise. This makes your new salary 55080.0 Judy, your current salary is 71000.00. You received a 2% raise. This makes your new salary 72420.0 Albert, your current salary is 38000.00. You received a 2% raise. This makes your new salary 38760.0 Alfonzo, your current salary is 42000.00. You received a 2% raise. This makes your new salary 42840.0
employeeDatabase = {
    'John': 54000.00,
    'Judy': 71000.00,
    'Albert': 38000.00,
    'Alfonzo': 42000.00
}

One of my many attempts (I'm realizing now that I should be saving previous attempts, I'm just using a random online IDE):

newdict = employeeDatabase.copy()

for x in newdict:
    newsal = newdict[x]
    newsal = newsal *.02 + newsal
    for i in employeeDatabase:
        print (i + ' your current salary is %s You received a 2%% raise. This makes your new salary %d'  % (employeeDatabase[i], newsal))

You don't need to use newdict in there, you can just use items to get the name and salary, and then print those 2 values plus the increased value. I've changed it to use the new string formatting syntax as well, since the old % style is falling out of use:

for employee, salary in employeeDatabase.items():
        print ("{}, your current salary is {:.2f}. You received a 2% raise. This makes your new salary {:.2f}".format(employee, salary, salary * 1.02))

Due to dict stucture, you can use keys to change data, so:

employeeDatabase = {
    'John': 54000.00,
    'Judy': 71000.00,
    'Albert': 38000.00,
    'Alfonzo': 42000.00
}
employeeDatabase['John'] = 58000.00

now John's salary is 58000

to raise all employeers salary by 2% do it:

def raise_salary(employeers):
    for i in employeers.keys():
        print(f'{i} your current salary is {employeers[i]} You received a 2% raise. This makes your new salary {employeers[i] + employeers[i] *.02}')

note: I'm using f-strings, this works on python3.6+

You can use the function .items() of dict objects in python. I gives you back a key-value pair that you can iterate with a for-loop. Also, by using the String function .format() you can set a variable with the default message and then fill the specific values (name, salary, etc.) inside the for-loop

message = '{}, your current salary is {}. You received a 2% raise. This makes your new salary {}'
employeeDatabase = {
    'John': 54000.00,
    'Judy': 71000.00,
    'Albert': 38000.00,
    'Alfonzo': 42000.00
}

for employee, salary in employeeDatabase.items():
    print(message.format(employee, salary, salary * 1.02))

the {} in the variable message indicate the places where you want to customize the default information with the one in your dict .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM