Iterating over the keys of a python dictionary, when keys are integers, I get this error, "TypeError: argument of type 'int' is not iterable"
Iterating over the keys of a python dictionary, when keys are integers, I get this error, "TypeError: argument of type 'int' is not iterable"
我正在研究 employees
的 pay raise
,特别是 ids
。假设,我公司有 5
名员工。我已经在 employee_id_list
中展示了它们。我希望 python 接受我的意见,其中包括 employees
的特定 ids
,我想提高工资,以及他们的 salary
。然后,我从这些输入中创建 dictionary
。现在我想遍历 employee_id_list
,使其与 input ids
相匹配。如果匹配,我想接受相应的 value of key
即 salary
并提高工资。但是我收到一个错误。
我已经搜索了 Whosebug 上的所有内容,但没有任何内容符合我的问题
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
是这个:
if e_ids in employee_dict[key] :
employee_dict
是 string-integer 对的字典,您正在尝试检查 e_ids
是否在 employee_dict[key]
中,这是一个 int
,不像列表那样可迭代,您可以在列表中检查元素是否包含在其中。
另外,你不是说employee_dict[key] = 0.8*value + value
吗?
您混淆了 for 循环的 key
和 value
变量
试试这个:
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)
你必须写 employee_dict[key] = 0.8*value+value
而不是 employee_dict[value] = 0.8*value+value
。
您也可以写 employee_dict[key] = value*1.8
而不是 employee_dict[key] = 0.8*value+value
。
把@Konny 和@Sunderam 的正确思路结合起来,加上我自己的改变来检查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)
我正在研究 employees
的 pay raise
,特别是 ids
。假设,我公司有 5
名员工。我已经在 employee_id_list
中展示了它们。我希望 python 接受我的意见,其中包括 employees
的特定 ids
,我想提高工资,以及他们的 salary
。然后,我从这些输入中创建 dictionary
。现在我想遍历 employee_id_list
,使其与 input ids
相匹配。如果匹配,我想接受相应的 value of key
即 salary
并提高工资。但是我收到一个错误。
我已经搜索了 Whosebug 上的所有内容,但没有任何内容符合我的问题
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
是这个:
if e_ids in employee_dict[key] :
employee_dict
是 string-integer 对的字典,您正在尝试检查 e_ids
是否在 employee_dict[key]
中,这是一个 int
,不像列表那样可迭代,您可以在列表中检查元素是否包含在其中。
另外,你不是说employee_dict[key] = 0.8*value + value
吗?
您混淆了 for 循环的 key
和 value
变量
试试这个:
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)
你必须写 employee_dict[key] = 0.8*value+value
而不是 employee_dict[value] = 0.8*value+value
。
您也可以写 employee_dict[key] = value*1.8
而不是 employee_dict[key] = 0.8*value+value
。
把@Konny 和@Sunderam 的正确思路结合起来,加上我自己的改变来检查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)