(May-20-2018, 05:38 AM)wavic Wrote: Yes, there is. This is what the assignment asks for. To store the countries in a list and the capitals in another list. Before the loop create two lists called for example 'countries' and 'capitals' and in every iteration append rec[0] to the 'countries' and rec[1] to the 'capitals'. But why 'rec'? Why not:country, capital = line.split(',')You are asking if there is a way to store the countries in a list. Do you know the append method? You create a list called foo and is empty. Then you can store something in it using its append method.foo.append('A')
I do not know the append method as you suggested.
Is it a storing method?
But now since you said, I will try and later share the code whether successful or not.
Thank you once again
(May-20-2018, 05:38 AM)wavic Wrote: Yes, there is. This is what the assignment asks for. To store the countries in a list and the capitals in another list.
Before the loop create two lists called for example 'countries' and 'capitals' and in every iteration append rec[0] to the 'countries' and rec[1] to the 'capitals'. But why 'rec'? Why not:
country, capital = line.split(',')You are asking if there is a way to store the countries in a list. Do you know the append method? You create a list called foo and is empty. Then you can store something in it using its append method.
foo.append('A')
print("No.\tCountries\t\t\t Capitals\n ") f = open(r'C:\Users\USER\Desktop\Notepad_Read\countries.txt') count = 0 foo = [""] data = f.readlines() for line in data: line = line.rstrip('\n') countries, capitals = line.split(',') #Changed count = count + 1 print("{0:1}\t{1:26}\t {2:24}".format(count,countries,capitals)) foo.append(countries) #Not sure how to store the countries inside here while True: line = str(input("Enter a country name (x to Exit): ")) if line == 'x': break elif line == countries: print ("The capital of {0} is{1}".format(countries,capitals)) else: print("The country is not in the list. Please try again.") f.close()Is this how I should do to make an empty list from the foo?
It still couldn't read any countries except for the last one.