惟余莽莽 发表于 2021-10-11 12:26:05

求助为什么会报错

river = { 'nile':'eygot','huang river':'china','mississippi river':'america'}
for key,value in river.items():
    print(f"The {key.title()} runs through {value.title()} ")
for river in sorted(river.keys()):
    print(river)
for nation in sorted(river.values()):
    print(nation)


为什么会报错呢?
下面两个for循环去掉一个就不报错了,又是为什么呢?

傻眼貓咪 发表于 2021-10-11 12:32:58

river = { 'nile':'eygot','huang river':'china','mississippi river':'america'}
for key,value in river.items():
    print(f"The {key.title()} runs through {value.title()} ")
for river in sorted(river.keys()): # 这里你用和字典同名(river)已经改变 river 值了,river 不再是字典了
    print(river)
for nation in sorted(river.values()):
    print(nation)

惟余莽莽 发表于 2021-10-11 12:39:04

傻眼貓咪 发表于 2021-10-11 12:32


豁然开朗,少打了一个s,谢谢
页: [1]
查看完整版本: 求助为什么会报错