国家,Python中的大写字典。用户输入国家、国家和资本输出

大家好,我正在努力解决这个国家资本问题,用户输入一个国家,国家和资本应该是产出。此外,即使用户输入西班牙的Sp,也会输出该单词的所有匹配项。到目前为止,我只能输出值,例如,如果用户输入西班牙,则弹出马德里,或者如果输入日本,则输出东京。任何帮助都将不胜感激。以下是指向该文件的链接
Https://drive.google.com/folderview?...2s&usp=sharing

我在寻找正确方向的提示,而不是答案,谢谢。

选择 | 换行 | 行号
  1. print()
  2.  
  3. print('Hello,this program is to match countries to their capitals'.title())
  4.  
  5. input_file=open('Country-Capital.csv','r')
  6.  
  7. country_capital=dict()
  8.  
  9. for line in input_file:
  10.  
  11. line=line.split(',')
  12.  
  13. countries=line[0]
  14.  
  15. capitals=str(line[1].strip('\n'))
  16.  
  17. Capitals=capitals.strip(';')
  18.  
  19. if countries in country_capital:
  20.  
  21.     country_capital[countries].append(Capitals)
  22. else:
  23.  
  24.     country_capital[countries]=[Capitals]
  25. user_input=''
  26.  
  27. print('Enter -1 to exit to see all Countries and Capitals')
  28.  
  29. print()
  30.  
  31. while user_input != '-1':
  32.  
  33. user_input=input('enter a country to get its capital: ').title()
  34.  
  35. print('You Entered: ',user_input)
  36.  
  37. if user_input in country_capital: 
  38.  
  39.     print('Answer: ',country_capital[user_input])
  40.  
  41.     print()
  42.  
  43.     print('Enter -1 to exit and to all Countries and Capitals')
  44.  
  45.     print()
  46. else:
  47.  
  48. print('GAME OVER')
  49.  
  50. print('    Here are the Countries and Capitals:')
  51.  
  52. print()
  53.  
  54. for key,value in country_capital.items():
  55.  
  56.    print('Country: ',key)
  57.  
  58.    print('Capital: ',value)
  59.  
  60.    print('    Here are the Countries and Capitals:')
  61. input_file.close()
  62.  
  63. and this is my output:
  64.  
  65. enter a country to get its capital: Spain
  66.  
  67. You Entered: Spain
  68.  
  69. Answer: ['Madrid']
  70.  
  71. Enter -1 to exit and to all Countries and Capitals
  72.  
  73. enter a country to get its capital:

标签: python

添加新评论