May-19-2018, 08:46 AM
(May-19-2018, 08:30 AM)wavic Wrote:>>> l = list('some data') >>> for element in l: ... print(element) ... s o m e d a t a >>> for element in l: ... print(element*4) ... ssss oooo mmmm eeee dddd aaaa tttt aaaaHere I loop again through the list to do something else. As I understand the assignment you have to have one list for the countries and one to store the capitals. But this is the mine interpretation.Quote:... to read the same into lists country and capital respectively
print("No.\tCountries\t\t\t Capitals\n ") f = open(r'C:\Users\USER\Desktop\Notepad_Read\countries.txt') count = 0 data = list(f.readlines()) for line in data: line = line.rstrip('\n') rec = line.split(',') count = count + 1 print("{0:1}\t{1:26}\t {2:24}".format(count,rec[0],rec[1])) while True: line = str(input("Enter a country name (x to Exit): ")) if line == 'x': break elif line == rec[0]: print ("The capital of {0} is{1}".format(rec[0],rec[1])) else: print("The country is not in the list. Please try again.") f.close()It still couldn't read ALL the countries except the "Russia".
Why is it only the last row be stored, and not ALL the rows in the text file?