python中使用while循环input方式来创建字典

我们可以通过input()方法来获取所需要创建字典的key和value值,并且把他们存储到字典中从而完成字典的创建。示例代码如下:

responses = {}
does_go_on = True
while does_go_on:
	name = input("What is your name?")
	response = input("Which mountain would you like to climb someday?")
	responses[name] = response
	ask = input("Would you like other people to respond?(yes/no)")
	if(ask == 'no'):
		does_go_on = False
	else:
		pass
for name in responses:
	message = name + " would like to climb " + responses[name]
	print(message)

我们在cmd窗口中运行后可以得到以下结果:
python中使用while循环input方式来创建字典_第1张图片

你可能感兴趣的:(python基础数据类型)