用Python编写一个程序,第一行是整数n,表示n个英文单词及其译文,接下来输入n行英文单词和译文,英文单词和译文用空格隔开。接下来输入的一行是一个英文单词。然后输出最后输入的英文单词的译文。如果没有检索到该单词,输出“not found”
时间: 2023-09-15 11:06:01 浏览: 117
### 回答1:
以下是Python程序的代码:
n = int(input())
dict = {}
for i in range(n):
word, translation = input().split()
dict[word] = translation
search_word = input()
if search_word in dict:
print(dict[search_word])
else:
print("not found")
### 回答2:
可以用字典来存储英文单词和对应的译文。首先根据输入的整数n创建一个空字典translations。然后使用一个循环,循环n次,读取n行输入,将单词作为键,译文作为值,保存到translations字典中。最后,读取最后一行输入的英文单词,从translations字典中检索该单词的译文并输出。
下面是一个用Python编写这个程序的示例代码:
```python
n = int(input()) # 读取整数n
translations = {} # 创建一个空字典
# 循环读取n行英文单词和译文,并保存到字典translations中
for i in range(n):
word, translation = input().split() # 读取一行英文单词和译文,用空格分隔
translations[word] = translation # 将单词和译文保存到字典中
search_word = input() # 读取最后一行输入的英文单词
# 检索该单词的译文并输出,如果没有检索到则输出“not found”
if search_word in translations:
print(translations[search_word])
else:
print("not found")
```
注意,该代码假设输入是正确的,即输入的整数n和英文单词和译文之间的格式是正确的。如果输入不符合要求,可能会导致程序出错。在实际应用中,可能需要添加一些错误处理的代码来处理这种情况。
### 回答3:
可以使用Python编写一个程序来实现上述功能。首先,可以通过input()函数获取输入的整数n。然后,可以使用一个字典来存储n个英文单词及其译文。
具体的代码如下:
```python
n = int(input()) # 输入整数n
word_dict = {} # 创建一个空字典
# 获取n个英文单词及其译文,并存入字典中
for _ in range(n):
word, translation = input().split() # 以空格分隔英文单词与译文
word_dict[word] = translation
search_word = input() # 获取要查询的英文单词
if search_word in word_dict:
print(word_dict[search_word]) # 输出查询到的单词的译文
else:
print("not found") # 单词没有找到,输出"not found"
```
通过以上代码,即可实现输入n个英文单词及其译文,然后查询指定英文单词的译文,并输出结果。如果找不到查询的单词,则输出"not found"。