gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:04:13 25 4
gpt4 key购买 nike

我正在为具有特定 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com