'Why isn't my variable accepting all values in my program
I'm making a program that calculates salary by taking in input from a .txt file. It opens it, then runs it through an if/else statement to see where it falls under and calculates a certain percentage increase based off the amount of money it is. I had 3 lists for all three parts, oldSalary, newSalary, and salaryList, but I had to remove salaryList because those were the instructions. I zipped oldSalary and newSalary so I could print them together, and I got the salaries to print next to it as well, but the issue is that it prints only the last value in that list, which in this case is '4', so it only says 4% when you do it, instead of 7%, 5.5%, etc. If you want to run it, the code is at https://pastebin.pl/view/06d10283 and the input is on https://pastebin.pl/view/40195d2c
The part of the code that I am using to print and format it is
for x,y in zip(oldSalaryList, newSalaryList):
print(x + '\t '+ y + '\t' + format((pay_raised),'.2f') + '%')
I'm not so sure that's the issue, but there it is. If anyone could help me with this, that would be greatly appreciated
Solution 1:[1]
pay_raised is set to the last value in the while loop.
With raises
If you wanted to change it according to the particular salary, your zip statement should look like this.
for x, y, raise in zip(oldSalaryList, newSalaryList, raises):
print(x + '\t ' + y + '\t' + raise)
NB: Your raises list is already formatted as a string with "%" etc.
Without raises :(
Having the raise list is a convenient solution to the problem. If you can't use that, you have to make your own.
for old_salary, new_salary in zip(oldSalaryList, newSalaryList):
raise = new_salary - old_salary
raise_in_percent = 100 * diff / old_salary
print(x + '\t ' + y + '\t' + format((raise_in_percent),'.2f') + '%')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
