-4

PLEASE HELP ME! I'M SUPER NEW AT THIS AND THINK I'VE GONE COMPLETELY OFF SOMEWHERE.

I Was given a csv file to use and extrapolate data from. Had a pretty extensive list of countries with their corresponding GDPs, unemployment rates and more. I'm mean to 1. Provide the country with the highest GDP in the list, and 2. Determine if this country has below average unemployment rate to go with it. I was able to get through the data definitions OK (I think) but I'm running into problems when I try to find the average unemployment rate in the given list (I used named tuple to organize the list by unemployment rate and country name).

def highest_gdp(logdp:List[CountryGDP])-> str: 
    """
    returns the name of the country with the highest GDP
    """
    #Return "" #body of the stub
    #template from List[CountryGDP] 

    highest = logdp[0] #type: CountryGDP
    for gdp in logdp:
        if gdp.cgdp >= highest.cgdp:
            highest = gdp
        return highest.name


def average_unemployment(louemp:List[Unemployment])-> float:
    """
    returns the average unemployment rate of the given list
    """
    #return 0 #body of the stub
    #template from List[Unemployment]

    average = louemp[0] #type: Unemployment
    for uemp in louemp:
        if uemp.rate > 0: 
            average = uemp
        return sum(uemp.rate)/float(len(louemp))

The first section is correct, I think. No errors were returned when I ran it, but I can't seem to figure out the second one that would be returning the average unemployment rate!! I keep getting various kinds of error messages, no matter what I try! I know this is super basic, but please help!!!

Sabin Chacko
  • 713
  • 6
  • 17
Solo
  • 1
  • 1
  • 1
    "Various kinds of error messages" for what code? Please limit your question to one specific question, as a question of "my code doesn't work" with an answer of "here's working code" is unlikely to help (or even be found by) future readers. – TigerhawkT3 Mar 30 '17 at 10:55
  • sorry, to the second one. Im mostly asking how i can find the average of my Unemployment list. Im running into trouble on the last two lines. I get error messages such as "'float' object is not iterable" or "only concatenate tuple (not "float") to tuple" with every change i make – Solo Mar 30 '17 at 11:09
  • Is this for an independent project, schoolwork, or a job-related task? If the first, spend some more time with tutorials before continuing. If the second, review your textbooks and discuss it with your teacher. If the third, tell your manager that they'll need to assign this task to one of the company's Python developers or give you a few weeks to learn Python. – TigerhawkT3 Mar 30 '17 at 11:18
  • `sum` takes something iterable to add up - you are giving it `uemp.rate` which I presume if just a number. hence float object is not iterable kinds of errors. – doctorlove Mar 30 '17 at 11:28

1 Answers1

0

I think you need to place the return statement outside of the for loop. For both cases. I didn't check the logic though.

def highest_gdp(logdp:List[CountryGDP])-> str: 
    """
    returns the name of the country with the highest GDP
    """
    #Return "" #body of the stub
    #template from List[CountryGDP] 

    highest = logdp[0] #type: CountryGDP
    for gdp in logdp:
        if gdp.cgdp >= highest.cgdp:
            highest = gdp
    return highest.name


def average_unemployment(louemp:List[Unemployment])-> float:
    """
    returns the average unemployment rate of the given list
    """
    #return 0 #body of the stub
    #template from List[Unemployment]

    average = louemp[0] #type: Unemployment
    for uemp in louemp:
        if uemp.rate > 0: 
            average = uemp
    return sum(uemp.rate)/float(len(louemp))
Sabin Chacko
  • 713
  • 6
  • 17