473,383 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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 2686

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

Similar topics

11
by: Sven Neuberg | last post by:
Hi, I have been handed the task of updating and maintaining a web application, written in ASP and Javascript, that takes complex user inputs in HTML form and submits them to server-side ASP...
2
by: Zyron | last post by:
Hello! I'm developing a web page that has a goal of providing personal content, which means that all information should be written in their own language. I'm about to finish my Geo targeting...
10
by: jeff regoord | last post by:
A user inputs a float value. The scanf() function gets the value. However, I need to create an error handler with an if else statement saying invalid input if the input is not a number. Does...
6
by: jrshack | last post by:
I have a web form that users input answers and when submitted I want the ASP code to place the user inputs into certain locations of a HTML form. The form is then automatically opened in a new...
2
jinalpatel
by: jinalpatel | last post by:
I have two tables MainFirm Name Address City State Zip County
6
by: gita ziabari | last post by:
Hello All, The following code does not work for unicode characters: keyword = dict() kw = 'генских' keyword.setdefault(key, ).append (kw) It works fine for inserting ASCII character. Any...
14
by: happyse27 | last post by:
Hi all, I did the null string comparison for below... But could not work as i keyed some value into the $company field but it did not print out okok, but not ok. I used if ($company != '')...
3
by: dinesh1985singh | last post by:
I want to get the exact time of user when he did comment. As the user belongs to different country so I want to get the exact time of his country just like in discussion form in PHP.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.