题解 | #梦想的大学#
字典的创建、字典键值对的添加、通过字典的键获得对应的值、输入的读取、while循环的终止、列表按升序排序
- 创建一个名为survey_dict的空字典,
- 编写一个循环,每次循环开始先使用print()输出一行'If you have the chance, which university do you want to go to most?',
- 再使用print()输出'What is your name?',再将读取到的字符串存储在变量name中,
- 再使用print()输出'Which university do you want to go to most?',再将读取到的字符串存储在变量university中,
- 再把键-值对name: university存储在字典survey_dict中,
- 再使用print()输出"Is there anyone who hasn't been investigated yet?",
- 如果输入的字符串为'No',则使用break语句退出循环,否则本次循环结束,再次进入while循环中的条件测试。
- 在while循环结束后,使用for循环遍历使用sorted()函数按升序进行临时排序的包含字典survey_dict的所有键的列表,
- 对于每一个遍历到的被调查者的名字,使用print()输出类似"I'am Tom. I'd like to go to Fudan University if I have the chance!"的语句。
survey_dict={}
while True:
print('If you have the chance, which university do you want to go to most?')
print('What is your name?')
name=input()
print('Which university do you want to go to most?')
university=input()
survey_dict[name]=university
print("Is there anyone who hasn't been investigated yet?")
if input()=='No':
break
for i in sorted(survey_dict.keys()):
print("I'am {}. I'd like to go to {} if I have the chance!".format(i,survey_dict[i]))