473,395 Members | 1,885 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,395 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 2688

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.
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.