Question

In: Computer Science

def compareRisk(compareCountry, countryList, filename):     filename = open(filename, "r")     content = filename.readlines()     returnList =...

def compareRisk(compareCountry, countryList, filename):
    filename = open(filename, "r")
    content = filename.readlines()
    returnList = []
    for row in content [1:]:
        line = row.split(",")
        name = line[0]
        population = float(line[1])
        infectedPopulation = float(line[2])

    for compareCountry in content:
        comparePopulation = float (line[1])
        compareInfected = float (line[2])

    for name in countryList:
        if population < comparePopulation:
            if infectedPopulation > compareInfected:
                returnList.append(name)
            else:
                return "No countries"
    return returnList
print(compareRisk("Tuvalu", ["Turkmenistan", "Norway", "Netherlands", "Philippines"], 'covid.csv'))
       


Why is my code returning an empty list?

This is the covid.cvs snippet for reference:

Country Cases Infected
Aruba 11883 10105
Afghanistan 36651 29167
Angola 4858 3321
Anguilla 16657 11785
Ã…land Islands 59569 18462
Albania 87629 37130
Andorra 18596 1813
United Arab Emirates 22831 5957
Argentina 14593 11424
Armenia 22058 10286
American Samoa 19845 16246
Antarctica 72622 26437
French Southern Territories 99456 12123
Antigua and Barbuda 53272 24205
Australia 72149 55762
Austria 35311 10826
Azerbaijan 9005 2600
Burundi 20872 20308
Belgium 54222 17084

Solutions

Expert Solution

check out the solution.

-----------------------------------------------------------

Code:

# function definition
def compareRisk(compareCountry, countryList, filename):
# open file in read mode
filename = open(filename, "r")
# read file contents
content = filename.readlines()
# initialize return list
returnList = []
  
# save the details of compareCountry
for row in content [1:]:
if compareCountry in row:
line = row.split(",")
comparePopulation = float(line[1])
compareInfected = float(line[2])
  
# loop through each country in countryList
for name in countryList:
# loop through file
for row in content [1:]:
# if name present in file then only split that row
if name in row:
line = row.split(",")
population = float(line[1])
infectedPopulation = float(line[2])
  
# after splitting the row
# compare the details with compareCountry details
if population < comparePopulation:
if infectedPopulation > compareInfected:
# if condition met then append to the list
returnList.append(name)
  
# check if list if empty or not
if(returnList):
return returnList # if not empty , print the list
else:
return "No countries" # if empty, print appropriate message


# function call and print the returned list
print(compareRisk("Andorra", ["Afghanistan", "Armenia", "Angola", "Anguilla"], 'covid.csv'))

-----------------------------------------------------------------

---------------------------------------------------------------------------------

OUTPUT :

-----------------------------------------------------------

COVID.CSV :

=======================================================


Related Solutions

original code: filename = "CCL-mbox-tiny.txt" with open(filename, 'r') as f: for line in f.readlines(): print(line.strip()) 1....
original code: filename = "CCL-mbox-tiny.txt" with open(filename, 'r') as f: for line in f.readlines(): print(line.strip()) 1. Now modify the code above to print the number of lines in the file, instead of printing the lines themselves. You'll need to increment a variable each time through the loop and then print it out afterwards. There should be **332** lines. 2. Next, we'll focus on only getting lines addressing `X-DSPAM-Confidence:`. We do this by including an `if` statement inside the `for` loop....
In python def lambda_2(filename): # Complete this function to read grades from `filename` and map the...
In python def lambda_2(filename): # Complete this function to read grades from `filename` and map the test average to letter # grades using map and lambda. File has student_name, test1_score, test2_score, # test3_score, test4_score, test5_score. This function must use a lambda # function and map() function. # The input to the map function should be # a list of lines. Ex. ['student1,73,74,75,76,75', ...]. Output is a list of strings in the format # studentname: Letter Grade -- 'student1: C' #...
def read_words(filename, ignore='#'): """ Read a list of words ignoring any lines that start with the...
def read_words(filename, ignore='#'): """ Read a list of words ignoring any lines that start with the ignore character as well as any blank lines. """ return ['a', 'z'] How would I code this in Python?
Please use python3 Create the function team_average which has the following header def team_average(filename): This function...
Please use python3 Create the function team_average which has the following header def team_average(filename): This function will return the average number of games won by the Red Sox from a text file with entries like this 2011-07-02 Red Sox @ Astros Win 7-5 2011-07-03 Red Sox @ Astros Win 2-1 2011-07-04 Red Sox vs Blue Jays Loss 7-9 This function should create a file object for the file whose name is given by the parameter filename. If the file cannot...
CODE BLOCK E import csv filename = "D:/python/Week8/files/green.csv" with open(filename) as file: data_from_file = csv.reader(file) header_row...
CODE BLOCK E import csv filename = "D:/python/Week8/files/green.csv" with open(filename) as file: data_from_file = csv.reader(file) header_row = next(data_from_file) for index,column_header in enumerate(header_row): print(index,column_header) How many COUMNS (not rows!) will be printed in the above code?
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the #file and return the contents.# # # - If the contents of the file can be interpreted as # an integer, return the contents as an integer. # - Otherwise, if the contents of the file can be # interpreted as a float, return the contents as a # float. # - Otherwise, return the contents of the file as a # string. #...
1) Show that if A is an open set in R and k ∈ R \...
1) Show that if A is an open set in R and k ∈ R \ {0}, then the set kA = {ka | a ∈ A} is open.
Is my proof that empty set is open and R is open correct?
Is that empty set is open and R is open correct? Give details Explaination.
we have defined open sets in R: for any a ∈ R, there is sigma >...
we have defined open sets in R: for any a ∈ R, there is sigma > 0 such that (a − sigma, a + sigma) ⊆ A. (i) Let A and B be two open sets in R. Show that A ∩ B is open. (ii) Let {Aα}α∈I be a family of open sets in R. Show that ∪(α∈I)Aα is open. Hint: Follow the definition of open sets. Please be specific and rigorous! Thanks!
Show that any open subset of R (w. standard topology) is a countable union of open...
Show that any open subset of R (w. standard topology) is a countable union of open intervals. Please explain how to do, I only understand why it is true. What is required to fully prove this. What definitions should I be using.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT