【Python 3.7】河流:创建一个字典,在其中存储三条大河流及其流经的国家。其中一个键 — 值对可能是 ‘nile’: ‘egypt’ 。
题目:河流:创建一个字典,在其中存储三条大河流及其流经的国家。其中一个键 —值对可能是 ‘nile’: ‘egypt’ 。
使用循环为每条河流打印一条消息,如“The Nile runs through Egypt.”。
使用循环将该字典中每条河流的名字都打印出来。
使用循环将该字典包含的每个国家的名字都打印出来
程序为:
rivers={'nile':'egypt','Yangtze river':'china','Yellow river':'china'}
for river,conutry in rivers.items():
print("The "+river.title()+" runs through "+conutry.title())
for river in rivers.keys():
print(river.title())
for conutry in set(rivers.values()):
print(conutry.title())
结果为:
The Nile runs through Egypt
The Yangtze River runs through China
The Yellow River runs through China
Nile
Yangtze River
Yellow River
Egypt
China