python - 迭代 python 字典的键,当键是整数时,我得到这个错误, "TypeError: argument of type ' int' 不可迭代”

标签 python django list dictionary while-loop

我正在为具有特定 ids员工加薪。假设,我的公司有 5 名员工。我在 employee_id_list 中显示了它们。我希望 python 从我那里获取输入,其中包含 employees 的特定 ids,我想提高工资以及他们的 salary .然后,我根据这些输入创建了 dictionary。现在我想遍历 employee_id_list,使其与 input ids 相匹配。如果匹配,我想获取相应的 value of key,即 salary 并提高薪水。但是我收到一个错误。 我已经搜索了 stackoverflow 上的所有内容,但没有任何内容符合我的问题

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)

我收到这个错误

TypeError: argument of type 'int' is not iterable

最佳答案

把@Konny 和@Sunderam 的正确思路放在一起,加上我自己的check if 语句的改动,答案是:

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)

关于python - 迭代 python 字典的键,当键是整数时,我得到这个错误, "TypeError: argument of type ' int' 不可迭代”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71266921/

相关文章:

python - 调整 numpy 或数据集大小的有效方法?

python - Pandas :一列的累计总和基于另一列的值

python - 聚合数据时无法获取 <class 'django.db.models.QuerySet' > 的 repr

python-3.x - 将 Pandas 数据框转换为包含 ID 和权重的元组列表

python - 在 2 个 python 列表之间找到 "overlap"

python - 我应该如何命名我的类和函数甚至字符串?

python - IntegrityError 主键在 django 中无效

django - 解读Django服务器请求日志

python - 制定通用模型保存方法

python - 如何在字典内的列表中查找值