Posts: 27
Threads: 4
Joined: May 2018
(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 aaaa Here 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?
Posts: 2,955
Threads: 48
Joined: Sep 2016
'data' is already a list. There is no need to make it one explicitly.
Line 19.
'rec' holds the last pear from the loop.
Posts: 27
Threads: 4
Joined: May 2018
(May-19-2018, 09:02 AM)wavic Wrote: 'data' is already a list. There is no need to make it one explicitly. Line 19. 'rec' holds the last pear from the loop.
Oh, now I get it.
Then is there a way to store in a column rather than just the last pear from the loop?
A way to hold all the Country rather than just Russia.
So sorry for keep asking.
Posts: 2,955
Threads: 48
Joined: Sep 2016
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')
Posts: 27
Threads: 4
Joined: May 2018
May-20-2018, 06:12 AM
(This post was last modified: May-20-2018, 06:12 AM by Leonzxd.)
(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.
Posts: 2,955
Threads: 48
Joined: Sep 2016
May-20-2018, 06:27 AM
(This post was last modified: May-20-2018, 06:29 AM by wavic.)
data = f.readlines()
countries = [] # this is the storage list for the countries. An empty list
capitals = [] # this one is for the capitals
for line in data:
line = line.rstrip('\n')
country, capital = line.split(',')
countries.append(country) # append 'country' to 'countries'.
capitals.append(capital) # append 'capital' to 'capitals' Now in the second part of the script, you can't use '==' operator to check if an object is in the list. Line 23. They are different types. One is a string and another list of strings. The way you do it is to use 'in'. Almost plain English.
elif line in countries:
Posts: 27
Threads: 4
Joined: May 2018
May-20-2018, 06:47 AM
(This post was last modified: May-20-2018, 06:47 AM by Leonzxd.)
(May-20-2018, 06:27 AM)wavic Wrote: data = f.readlines() countries = [] # this is the storage list for the countries. An empty list capitals = [] # this one is for the capitals for line in data: line = line.rstrip('\n') country, capital = line.split(',') countries.append(country) # append 'country' to 'countries'. capitals.append(capital) # append 'capital' to 'capitals' Now in the second part of the script, you can't use '==' operator to check if an object is in the list. Line 23. They are different types. One is a string and another list of strings. The way you do it is to use 'in'. Almost plain English. elif line in countries:
It stored. But it doesn't come out the output as I thought.
It only pops "The capital of Russia is Moscow" whenever I typed any other countries.
This is the code:
print("No.\tCountries\t\t\t Capitals\n ")
f = open(r'C:\Users\USER\Desktop\Notepad_Read\countries.txt')
count = 0
data = f.readlines()
countries = [] #Empty list
capitals = [] #Empty list
for line in data:
line = line.rstrip('\n')
country, capital = line.split(',')
countries.append(country) #Country are stored in countries list
capitals.append(capital) #Capital are stored in capitals list
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 in countries:
print ("The capital of {0} is{1}".format(country,capital))
else:
print("The country is not in the list. Please try again.")
f.close() Output: Enter a country name (x to Exit): Malaysia
The capital of Russia is Moscow
Posts: 2,955
Threads: 48
Joined: Sep 2016
Again, you use 'country' and 'capital' with their values from the last loop. They are not changed. They were temporary objects and you can throw them away.
Now you have to deal with the lists 'countries' and 'capitals'.
Posts: 27
Threads: 4
Joined: May 2018
May-20-2018, 07:15 AM
(This post was last modified: May-20-2018, 07:15 AM by Leonzxd.)
(May-20-2018, 06:58 AM)wavic Wrote: Again, you use 'country' and 'capital' with their values from the last loop. They are not changed. They were temporary objects and you can throw them away. Now you have to deal with the lists 'countries' and 'capitals'.
Oh okay, so it was the old 'Country' and 'Capital', not the new listed ones...
So now I need to get the data from the list countries and capitals right?
How should I take the specific data from the list...?
Is there any code for it?
Edit: Also could throw away the old data?
Edit: Which means the logic for now is, throw away old data, use the data in the list array, extract from it, print it. ?
Posts: 2,955
Threads: 48
Joined: Sep 2016
You have to get a data only from the 'capitals' list if the input country is there.
If you put:
print(countries)
print(capitals)
in the alif block, you will see that they are in order.
elif line in countries:
print(countries)
print(capitals) If the input country is in the list 'counties' you get the index of the country in that list and print the capital with the corresponding index in the 'capitals' list.
|