Home > Net >  Iterating over the keys of a python dictionary, when keys are integers, I get this error, "Type
Iterating over the keys of a python dictionary, when keys are integers, I get this error, "Type

Time:02-26

I am working on pay raise of employees with particular ids. Suppose, I have 5 employees in my company. I have shown them in a employee_id_list. I want python to take input from me which consists of the particular ids of the employees, I want to raise the pay, along with their salary. Then, I am creating the dictionary out of these inputs. Now I want to iterate over the employee_id_list such that it matches with of the input ids. If it matches, I want to take the respective value of key which is salary and raise the pay. But I am getting an error. I have searched for everything present on stackoverflow but nothing matches my problem

employee_id_list = [27, 25, 98, 78, 66]
employee_dict = dict()
while True:
    x = input("Enter an key to continue and 'r' for result: ").lower()
    if x== 'r':
        break
    try:
        employee_id = int(input("Enter key the Employee id: ")) 
        salary = int(input(f"Enter the {employee_id}'s salary: "))
        employee_dict[employee_id] = salary
    except ValueError:
        print("Please Enter the Integers!")
        continue 
print(employee_dict)
for e_ids in employee_id_list:
    for key, value in employee_dict.items():
        if e_ids in employee_dict[key] :
            employee_dict[value] = 0.8*value   value
print(employee_dict)

I am getting this error

TypeError: argument of type 'int' is not iterable

CodePudding user response:

Putting the correct ideas of @Konny and @Sunderam together, and adding my own change for checking if statement, the answer is:

employee_id_list = [27, 25, 98, 78, 66]
    employee_dict = dict()

    while True:
        x = input("Enter an key to continue and 'r' for result: ").lower()
        if x== 'r':
            break
        try:
            employee_id = int(input("Enter key the Employee id: "))
            salary = int(input(f"Enter the {employee_id}'s salary: "))
            employee_dict[employee_id] = salary
        except ValueError:
            print("Please Enter the Integers!")
            continue

    print(employee_dict)
    for e_ids in employee_id_list:
        for key, value in employee_dict.items():
            if e_ids == key:    # This is my contribution
                employee_dict[key] = 1.8*value
    print(employee_dict)

CodePudding user response:

It's this:

if e_ids in employee_dict[key] :

employee_dict is a dictionary of string-integer pairs, and you are trying to check whether e_ids is in employee_dict[key], which is an int, not an iterable like a list, where you can check if an element is contained in it.

Also, don't you mean employee_dict[key] = 0.8*value value?

CodePudding user response:

You confused with key and value variable of for loop

Try this:

employee_id_list = [27, 25, 98, 78, 66]
employee_dict = dict()
while True:
    x = input("Enter an key to continue and 'r' for result: ").lower()
    if x == 'r':
        break
    try:
        employee_id = int(input("Enter key the Employee id: "))
        salary = int(input(f"Enter the {employee_id}'s salary: "))
        employee_dict[employee_id] = salary
    except ValueError:
        print("Please Enter the Integers!")
        continue
for e_ids in employee_id_list:
    for key, value in employee_dict.items():
        if e_ids in employee_dict:
            employee_dict[key] = 0.8*value   value
print(employee_dict)

You have to write employee_dict[key] = 0.8*value value rather than employee_dict[value] = 0.8*value value.

You can also write employee_dict[key] = value*1.8 rather than employee_dict[key] = 0.8*value value.

  • Related