6-2 喜欢的数字:
代码如下:
people={'Lily':'233','Jojo':'3','Bob':'5','admin':'7','Mike':'666'}
for name in people.keys():
print(name+"'s favorite number is "+people[name]+".")
运行结果:
6-5 河流:
代码如下:
people={'Nile River':'Egypt','Amazon River':"Venezuela","Yangtze River":"China"}
for name in people.keys():
print("The "+name+" runs through "+people[name]+".")
for name in people.keys():
print(name,end=" ")
print("")
for name in people.values():
print(name,end=" ")
print("")
结果如下:
6-6 调查:
代码如下:
people={'jen':'python','sarah':"c","edward":"ruby","phil":"python"}
all_people=['jojo','bob','jen','sarah','edward','mike','phil']
for name in all_people:
if name in people.keys():
print("Thank you for participation, "+name.title()+".")
else:
print("Would you want to have a test, "+name.title()+"?")
结果如下:
6-8 宠物:
代码如下:
Wang_Cai={"type":"dog","owner":"jojo"}
Xiao_Hei={"type":"cat","owner":"bob"}
pets=[Wang_Cai,Xiao_Hei]
for pet in pets:
print("The type of this pet is "+pet["type"]+" and its owner is "+pet["owner"]+".")
结果如下:
6-9 喜欢的地方:
代码如下:
Blacks=["Guangzhou","Tokyo","London"]
Heis=["Shanghai","L.A"]
Bais=["Beijing","California"]
favourite_places={'Black':Blacks,'Hei':Heis,'Bai':Bais}
for name in favourite_places.keys():
print(name+"wants to visit",end="")
for place in favourite_places[name]:
print(" "+place,end="")
print(".")
结果如下: