输入字符串查找列表并返回该值和另一个列表的相关索引

时间:2017-11-01 04:17:15

标签: python

# Welcome Message
print("Welcome to the Country and Capital look up program. If you enter a country found on our list"
      "\nit will display the capital to you. If you enter a Country that's not on the list it'll prompt"
      "\nyou to try another search.\n")

# Defining the function main. Not sure what to put under it so I have a blank print statement
def main():
    print()

# My assignment is to create 2 separate blank lists from the text file that has country,capital
countryList = []
capitalList = []

# The with open allows for the file to be auto closed once no longer being called
# Per the assignment I'm using the split command to remove the comma separating the country and capital
with open (r'D:\Python\Countries_capitals_20Rows.txt','r') as f:
    lines = f.readlines()
    for line in lines:
        country, capital = line.replace("\n","").split(",")
        countryList.append(country)
        capitalList.append(capital)

# The most non-pythonic way of making this program work since I couldn't get it to work by taking a input string and searching in the countryList and then finding the matching value or same index reference in the capitalList
while True:
    answer = (str(input("\nEnter a Country name to find it's Capital: "))) # ask user for the name of a country
    if answer == 'Afghanistan':
        print("The capital of Afghanistan is Kabul")
        break
    elif answer == 'Bahamas':
        print("The capital of Bahamas is Nassau")
        break
    elif answer == 'Cuba':
        print("The capital of Cuba is Havana")
        break
    elif answer == 'Denmark':
        print("The capital of Denmark is Copenhagen")
        break
    elif answer == 'Egypt':
        print("The capital of Egypt is Cairo")
        break
    elif answer == 'France':
        print("The capital of France is Paris")
        break
    elif answer == 'Germany':
        print("The capital of Germany is Berlin")
        break
    elif answer == 'Hungary':
        print("The capital of Hungary is Budapest")
        break
    elif answer == 'India':
        print("The capital of India is New Delhi")
        break
    elif answer == 'Japan':
        print("The capital of Japan is Tokyo")
        break
    elif answer == 'Kenya':
        print("The capital of Kenya is Nairobi")
        break
    elif answer == 'Mexico':
        print("The capital of Mexico is Mexico City")
        break
    elif answer == 'Norway':
        print("The capital of Norway is Oslo")
        break
    elif answer == 'Puerto Rico':
        print("The capital of Puerto Rico is San Juan")
        break
    elif answer == 'Qatar':
        print("The capital of Qatar is Doha")
        break
    elif answer == 'Russia':
        print("The capital of Russia is Moscow")
        break
    elif answer == 'South Korea':
        print("The capital of South Korea is Seoul")
        break
    elif answer == 'Thailand':
        print("The capital of Thailand is Bangkok")
        break
    elif answer == 'United States':
        print("The capital of United States is Washington DC")
        break
    elif answer == 'Venezuela':
        print("The capital of Venezuela is Caracas")
        break
    elif answer == 'Zimbabwe':
        print("The capital of Zimbabwe is Harare")
        break

# My notes regarding the method I tried to get working that did not work
"""
Beginning of Loop
I first had a print("Found it") until I could figure how to print the country from countryList and capital from
capitalList. For some reason when I enter print(country,capital) it prints the last entry in the countryList and
capitalList. I also created a for country, capital in sorted(zip) that displays them both side by side, but can't
figure how to search for a user input in a list and index it so that it knows how to pull the same index number from
the other list. I reviewed so much code online for over 7 hours this week and found no one doing that. I searched
my class notes for the list n lists and didn't see how to make it work based on a user input, only entering the index
number manually. So I'm commenting out my code below and going to write a if, elif for each option

while True:
    answer = (str(input("\nEnter a Country name to find it's Capital: "))) # ask user for the name of a country
    if answer in countryList:
        print(country, capital)
        break
    print("\nSorry, the Country you entered is not in my database. Please try enter another Country from the list below:\n")
    print(countryList)
    print(capitalList)
    for country, capital in sorted(zip(countryList, capitalList)):
        print(country, capital)
"""

# Allows for the function to be defined above or below the call statement
if __name__ == "__main__": main()

0 个答案:

没有答案