正在学习英语的牛妹笔记本上准备了这样一个字典:{'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}。
请你创建这样一个字典,对于牛妹输入的字母,查询有哪些单词?
输入一个字母,必定在上述字典中。
同一行中依次输出每个单词,单词之间以空格间隔。
a
apple abandon ant
d = {'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'} word = input() if isinstance(d[word],list): for i in d[word]: print(i,end=' ') else: print(d[word])
dic = { "a": ["apple", "abandon", "ant"], "b": ["banana", "bee", "become"], "c": ["cat", "come"], "d": "down", } letter = input() try: # 输入d 输出d o w n 也通过了,有问题 if type(dic[letter]) == list: # 或者判断键值对的值的长度 len(dic[letter]) # 如果为1,则直接输出 for word in dic[letter]: print(word, end=" ") else: print(dic[letter]) except: print("输入的字母不在字典内")
English_dict = {'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'} letter_input = input() for letter in English_dict: if letter_input == letter: for word in English_dict[letter]: print(word,end=' ') #3,4两步合一 if letter_input in English_dict.keys()