469,325 Members | 1,502 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,325 developers. It's quick & easy.

Country, Capital dictionary in Python. User Inputs country, Country and capital outpu

Hello everyone I am struggling on this country capital problem where the user inputs a country and the country and capital are suppose to be the output. Also even if the user enters Sp for spain, all occurrences of that word are suppose to be outputted. So far I am able to only output the value, such as if the user enters spain, Madrid pops up or if Japan is entered, Tokyo is outputted. Any help would be appreciated. Here is a link to the file https://drive.google.com/folderview?...2s&usp=sharing.
Im looking for tips in the right direction and not the answer thank you.
Expand|Select|Wrap|Line Numbers
  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:
May 6 '15 #1
0 1485

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by Sven Neuberg | last post: by
6 posts views Thread by gita ziabari | last post: by
14 posts views Thread by happyse27 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.