国家,Python中的大写字典。用户输入国家、国家和资本输出
大家好,我正在努力解决这个国家资本问题,用户输入一个国家,国家和资本应该是产出。此外,即使用户输入西班牙的Sp,也会输出该单词的所有匹配项。到目前为止,我只能输出值,例如,如果用户输入西班牙,则弹出马德里,或者如果输入日本,则输出东京。任何帮助都将不胜感激。以下是指向该文件的链接
Https://drive.google.com/folderview?...2s&usp=sharing
。
我在寻找正确方向的提示,而不是答案,谢谢。
选择 | 换行 | 行号
- print()
- print('Hello,this program is to match countries to their capitals'.title())
- input_file=open('Country-Capital.csv','r')
- country_capital=dict()
- for line in input_file:
- line=line.split(',')
- countries=line[0]
- capitals=str(line[1].strip('\n'))
- Capitals=capitals.strip(';')
- if countries in country_capital:
- country_capital[countries].append(Capitals)
- else:
- country_capital[countries]=[Capitals]
- user_input=''
- print('Enter -1 to exit to see all Countries and Capitals')
- print()
- while user_input != '-1':
- user_input=input('enter a country to get its capital: ').title()
- print('You Entered: ',user_input)
- if user_input in country_capital:
- print('Answer: ',country_capital[user_input])
- print()
- print('Enter -1 to exit and to all Countries and Capitals')
- print()
- else:
- print('GAME OVER')
- print(' Here are the Countries and Capitals:')
- print()
- for key,value in country_capital.items():
- print('Country: ',key)
- print('Capital: ',value)
- print(' Here are the Countries and Capitals:')
- input_file.close()
- and this is my output:
- enter a country to get its capital: Spain
- You Entered: Spain
- Answer: ['Madrid']
- Enter -1 to exit and to all Countries and Capitals
- enter a country to get its capital: