我目前正在进行的练习有三种不同的词典。我想要做的是用其他两个中的键和值更新第一个字典,并使用两个不同的for循环。
def give_me_new(dic1, dic2, dic3):
for key, value in dic2.items():
dic1.update({key:value})
for k, v in dic3.items():
dic1.update({k:v})
更新后,我将所有键放在变量中。
some_keys = dic1.keys()
我决定做的是利用while循环检查字符内是否存在密钥。我像这样创建了它
value = False
while value == False:
check_key = input('Lets check to see if key has been updated: ')
if check_key not in some_keys:
input('Nope, not in there. Press enter to try again')
elif check_key in some_keys:
return 'It is in there', dic1
value = True
但是,每次提示输入并输入值时,它都会一直显示该键不在其中。在导致此问题出现的整个函数中究竟发生了什么?
下面是创建的整个脚本,请让我知道我做错了什么。
def give_me_new(dic1, dic2, dic3):
for key, value in dic2.items():
dic1.update({key:value})
for k, v in dic3.items():
dic1.update({k:v})
some_keys = dic1.keys()
value = False
while value == False:
check_key = input('Lets check to see if key has been updated: ')
if check_key not in (some_keys):
input('Nope, not in there. Press enter to try again')
elif check_key in (some_keys):
return 'It is in there', dic1
value = True
print(give_me_new({1:10, 2:20}, {3:30, 4:40}, {5:50,6:60}))