有人可以帮我理解在这段代码中是如何使用“if”语句的吗?这是 LearnStreet 系列 Python 课程中的一个练习:http: //www.learnstreet.com/lessons/study/python#get-hint
我认为“if”语句需要一个布尔条件:如果 X < Y,则执行 Z。但我不明白布尔值如何成为这些“if”语句的一部分。
def run():
smiths = {"father": "Mike", "ex-wife" : "Mary", "children" : ["Bobby", "Susan"] }
jones = {"mother": "Lucy", "ex-husband": "Peter", "children": ["Michelle", "Jeff", "Evan"]}
family = {}
for key in smiths:
if key in family:
family[key]+=smiths[key]
else:
family[key]=smiths[key]
for key in jones:
if key in family:
family[key]+=jones[key]
else:
family[key]=jones[key]
keysToDel = []
for key in family:
if 'ex' in key:
keysToDel.append(key)
print keysToDel
for key in keysToDel:
del family[key]
return family