May-18-2018, 03:35 PM
Hi once again...
Edit: As previously mentioned, the notepad consists of 12 records of countries such as:
Malaysia, Kuala Lumpur
Japan, Tokyo
United States of America, Washington
Canada, Ottawa
Pakistan, Islamabad
Australia, Canberra
Mexico, Mexico City
Thailand, Bangkok
Taiwan, Taipei
China, Beijing
Czech Republic, Prague
Russia, Moscow
Thank you for helping me with the previous post.
However, right now I would like to search a COUNTRY in the text file (Notepad).
This is the best code I can think of.
But it wouldn't let me read the data I stored.
Please help once again...
Thank you!
I changed here:
Need Help!
Edit: As previously mentioned, the notepad consists of 12 records of countries such as:
Malaysia, Kuala Lumpur
Japan, Tokyo
United States of America, Washington
Canada, Ottawa
Pakistan, Islamabad
Australia, Canberra
Mexico, Mexico City
Thailand, Bangkok
Taiwan, Taipei
China, Beijing
Czech Republic, Prague
Russia, Moscow
Thank you for helping me with the previous post.
However, right now I would like to search a COUNTRY in the text file (Notepad).
This is the best code I can think of.
print("No.\tCountries\t\t\t Capitals") f = open(r'C:\Users\USER\Desktop\Notepad_Read\countries.txt') count = 0 for line in f: line = line.rstrip('\n') rec = line.split(',') country = rec[0] capital = rec[1] count = count + 1 print("{0:1}\t{1:26}\t {2:24}".format(count,country,capital)) while True: line = str(input("Enter a country name (x to Exit):")) if line == 'x': break elif line == country: #Read the country name list = line.split(',') print ("The capital of {1} is {2}".format(country,capital)) else: list = line.split(',') print("The country is not in the list. Please try again.") f.close()I want to INPUT a country name and show it out its capital.
But it wouldn't let me read the data I stored.
Please help once again...
Thank you!

I changed here:
elif line == country: list = line.split(',') print ("The capital of {0} is {1}".format(country,capital))However, it only seems to let me read Russia, but other countries I couldn't pop out the answer.
Need Help!