学python的书里面有这么一段代码:
responses = { } #设置标志 polling_active = True while polling_active: name = input("\nwhat is your name ") response = input("\nwhich mountain would you like to climb someday ") responses[name] = response repeat = input("\nwould you like to ler another person respond? (yes/no)") if repeat == 'no': polling_active = False print("\n ------Polling Result-----") for name,response in responses.items(): print(name + " would like to climb " + response + ".")
responses[name] = response
这段代码原书中写的是#将答案存储在字典中
一开始没有搞懂这个含义,为什么要这么写,后来明白了:
responses(字典)⬅ 存储⬅ [name](键值)⬅ = (赋值)⬅response(value 值)
这句话要从右往左理解,通过将response获得的输入值,赋值到responses字典的name键值上,所以是这么写的!