Hey
I am looking to improve my working function below;
The objective is to get back a list of countries from the free api.
The argument parsed is the number of countries the user wants back , the default is ALL the countries .
Two ways i want to improve is ;
a)i want the len(z) , the maximum value to be defined within the loop instead of explicitly defining it as 250
this would be in case countries are added
b) I want an object list 'country_list' , which is appended to in this code, to be created by this function , and not only just as output displayed when running the function
--------start of function -----------------------
test case 1 : list_of_countries(4)
test case 2 : list_of_countries() #without explicitly using 250 in default
test case 3 : print(country_list)
I am looking to improve my working function below;
The objective is to get back a list of countries from the free api.
The argument parsed is the number of countries the user wants back , the default is ALL the countries .
Two ways i want to improve is ;
a)i want the len(z) , the maximum value to be defined within the loop instead of explicitly defining it as 250
this would be in case countries are added
b) I want an object list 'country_list' , which is appended to in this code, to be created by this function , and not only just as output displayed when running the function
--------start of function -----------------------
def list_of_countries(number_to_return=250):
allurl='https://restcountries.eu/rest/v2/all'
uh=urllib.request.urlopen(allurl)
data = uh.read().decode()
z=json.loads(data)
lim=number_to_return
country_list=list()
i=0
while i < lim :
y=z[i]
country_list.append(y['name'])
i+=1
return country_list-------------------------end of function---------------------test case 1 : list_of_countries(4)
test case 2 : list_of_countries() #without explicitly using 250 in default
test case 3 : print(country_list)
