Home > front end >  How can I create a pro-rated salary scale dictionary using a for loop?
How can I create a pro-rated salary scale dictionary using a for loop?

Time:08-24

somewhat new to Python here and wondering what the best way to accomplish this task would be. I am trying to create a dictionary that represents a pro-rated salary scale for a certain amount of days.

For example; day 1 salary = 1017781 and reduces by 5849 for each day after for a total of 174 days.

So the dictionary's key:value pairs would be formatted as {day: salary}

The first three key:value pairs should look like:

{1: 1017781, 2: 1011932, 3: 1006082}

So far I've tried something like:

dicts = {}
total_days = range(174)
day = 1
salary = 1017781

for i in total_days:
    day  = 1
    salary -= 5849
    dicts[day] = dicts[salary]

But I am getting KeyError: 1011932

Any solutions to creating this dictionary?

CodePudding user response:

You can use a dictionary comprehension (fancy way of creating a dictionary)

{day: 1017781 - 5849 * day for day in range(1, 175)}

CodePudding user response:

Typo: I think it should be salary instead of dicts[salary]. Also, I would move the logic to increment / decrement the variables until the end of the for loop - see below.

from pprint import pprint

dicts = {}
total_days = range(174)
day = 1
salary = 1017781

for i in total_days:
    dicts[day] = salary
    day  = 1
    salary -= 5849

pprint(dicts)

Out:

{1: 1017781,
 2: 1011932,
 3: 1006083,
 ...}

Minor optimization when using range(start, stop, step):

dicts = {}
total_days = range(1, 175)
salary = 1017781

for day in total_days:
    dicts[day] = salary
    salary -= 5849

And, one step further with a list comprehension:

salary_delta = 5849
salary_start = 1017781   salary_delta

dicts = {day: salary_start - salary_delta * day for day in range(1, 175)}

CodePudding user response:

At, firs the value of the third key in your dictionary is wrong. It is going to be 1006083. Now coming to your problem, you need to understand how you create a new key in a dictionary and assign value to it. When you wrote dicts[day] = dicts[salary] Your dictionary created a new key called '2' but on the other side of the equal sign, you didn't assign a value, rather, you defined a new key that doesn't exist. So, the problem was you were assigning a key to a key. When you assign a value to a key, you simply write the value or the variable that contains a value, for example, dicts[day] = salary Now another problem was you had raised your variable's(Day) value by 1 before it got printed. So, you got the dictionary key from '2', not '1' and the same goes with the second value. So, you should be taking care of that as well.

dicts = {}
total_days = range(174)
day = 1
salary = 1017781
for i in total_days:
    dicts[day] = salary
    day  = 1
    salary -= 5849

CodePudding user response:

Here I have create the dynamic loop, which appling the changes on last items of dictionary and updating that changes.

dic = {1:1017781}

for i in range(174-len(dic)):
    dic.update({list(dic.items())[-1][0] 1 : list(dic.items())[-1][1]-5849})

Output:

{1: 1017781,
 2: 1011932,
 3: 1006083,
 4: 1000234,
 5: 994385,
 6: 988536,
 7: 982687,
 8: 976838,
 9: 970989,
 10: 965140,
 11: 959291,
 12: 953442,
 13: 947593,
 14: 941744,
 15: 935895,
.
.
.
.
  • Related