【发布时间】:2021-07-02 22:29:43
【问题描述】:
我坚持使用 python 字典,感谢您的帮助,因为我没有在互联网上找到任何类似问题的示例。
我正在尝试创建一个允许的程序
- 在用户输入字段中输入多个键
- 然后打印出在一个字符串中输入的键的所有值。
例如,如果用户输入“country1, country2, country3”,程序会打印出“'location1', 'location2', 'location3'。
在下面的代码中,它只允许一个国家和打印出一个位置。我尝试了使用列表、字典、元组的不同方法 - 无法弄清楚。
country_dict = {
'country1': 'location1',
'country2': 'location2',
'country3': 'location3',
}
country = input("Enter country: ")
if country.lower() == 'country1':
country_dict['country1']
if country.lower() == 'country2':
country_dict['country2']
if country.lower() == 'country3':
country_dict['country3']
print (country_dict[country])
【问题讨论】:
标签: python list dictionary tuples key