我得到了这个练习:创建一个获取人员字典的函数,并将parentFIrstName作为字符串。该函数将在字典中搜索父项,并将返回true或false-如果存在或不存在。 我写了一个代码,但它只给我个人字典和一个parentFIrstName作为字符串,但没有给我True或False。有什么问题吗?
person = {'Firstname': "christina", 'Lastname': "hidden from stackoverflow", 'Email': "hidden from stackoverflow" ,'Phone':"hidden from stackoverflow"}
parent = {'Firstname': "galina", 'Lastname': "hidden from stackoverflow", 'Email': "hidden from stackoverflow" ,'Phone':"hidden from stackoverflow"}
def personAndParentFirstName():
print(person, parent['Firstname'])
for i in person:
if (i == parent['Firstname']):
return True
return False
personAndParentFirstName()
[Running] python -u "c:\Users\darks\Documents\HELLO\test\test2.py"
{'Firstname': 'christina', 'Lastname': 'hidden from stackoverflow', 'Email': 'hidden from stackoverflow', 'Phone': 'hidden from stackoverflow'} galina
[Done] exited with code=0 in 0.139 seconds
答案 0 :(得分:1)
正如quamrana所说,您需要将其打印出来:
var id = ((IDictionary<string, object>)book)["@id"];
答案 1 :(得分:1)
解决方案1:
def personAndParentFirstName():
print(person, parent['Firstname'])
if person['Firstname'] == parent['Firstname']:
return True
else:
return False
print (personAndParentFirstName())
解决方案2:
def personAndParentFirstName():
print(person, parent['Firstname'])
for i in person.values():
if (i == parent['Firstname']):
return True
else:
return False
print (personAndParentFirstName())
注意:您不能像这样遍历字典值:
for i in person:
有多种方法可以迭代Python中的字典。
遍历所有键
遍历所有值
遍历所有键,值对
注意:函数返回的是true或false,但是您没有使用它。功能打印结果
print (personAndParentFirstName())