假设字典变量 dic _ country 存储了部分国家的国家名与首都名的对应关系,其中国家名为键,首都名为值。 dic _ country ={" China ":" Beijing "." America ":" Washington "," Norway "." Oslo "," Japan ":" Tokyo "." Germany "." Berlin "," Canada "." Ottawa "," France "." Paris "," Thailand "." Ban gkok ") 编写程序,根据用户输入的国家名查询首都名,如果存在则输出查询结果,,否则提示"未查询该国家名!"。假设对国家名进行查询不区分大小写
时间: 2023-06-28 08:13:48 浏览: 364
各个国家首都
可以使用以下代码实现:
```python
dic_country = {"China": "Beijing", "America": "Washington", "Norway": "Oslo", "Japan": "Tokyo", "Germany": "Berlin", "Canada": "Ottawa", "France": "Paris", "Thailand": "Bangkok"}
country = input("请输入国家名:").strip().lower() # 将输入的字符串去除首尾空格并转换为小写
capital = dic_country.get(country) # 使用 get 方法获取对应的首都名
if capital:
print("该国家的首都是:", capital)
else:
print("未查询该国家名!")
```
示例输出:
```
请输入国家名:china
该国家的首都是: Beijing
```
```
请输入国家名:USA
未查询该国家名!
```
阅读全文