'How can I loop salary amount against each employees? [duplicate]
I want my python to print the name of employees one by one (in a loop) and let me add the salary amount against each employee and in the last create a dictionary (key-value) for me, the key is employee name and value is amount and I also want to print out the total amount of salary means the sum of all the salaries.
How can I do that
That is what I did so far:
employees = ["abc", "def", "ghi", "jkl", "mno", "pqr"]
n=0
amonut= 0
try:
for employee in employees:
employee = employees[n]
amount= int(input(amount))
total = sum(amount)
n=n+1
print(total)
except:
print("You can only type integer for amount")
I have no idea how to create a dictionary in the last
Solution 1:[1]
I mean... something like this?
employee_names = ["abc", "def", "ghi", "jkl", "mno", "pqr"]
employee_salaries = {}
for employee in employee_names:
while True: # Input validation loop
try:
employee_salaries[employee] = int(input(f"Enter {employee}'s salary: "))
break
except ValueError:
print("Invalid input")
print(employee_salaries)
total = sum(employee_salaries.values())
print(total)
Solution 2:[2]
You don't need the n variable here. You can create a dictionary and add employee and amount to that
employees = ["abc", "def", "ghi", "jkl", "mno", "pqr"]
employees_dictionary = dict()
total = 0
amount = 0
try:
for employee in employees:
amount= int(input(amount))
employees_dictionary[employee] = amount
total += amount
except:
print("You can only type integer for amount")
print(employees_dictionary)
print(total)
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 | AKX |
| Solution 2 |
