What is the syntax error in my function?

Asked

Viewed 37 times

-5

def Grow_population_rate(year_1, year_2, City_name, pop_1, pop_2):
    pop_diff = pop_2 - pop_1
    year_diff = year_2 - year_1
    for pop_rate = pop_diff/year_diff * 100:
        return("The rate of population growth "+"City_name "+" in the year is "+str(pop_rate)+" %!")
  • 1

    Welcome to Stackoverflow. Please read the description of tag revisão-de-código, which explicitly states that the tag should only be used in codes that are working. As you are new to the site, it is normal you do not yet know the rules of the site, but precisely why I suggest you read the Stackoverflow Survival Guide in Portuguese, which will give you important instructions and information about the operation of Sopt. :)

2 answers

1

Carlos,

The for is not correct.

I believe that what you want is the below.

def Grow_population_rate(year_1, year_2, City_name, pop_1, pop_2):
    pop_diff = pop_2 - pop_1
    year_diff = year_2 - year_1
    pop_rate = pop_diff/year_diff * 100:
    return("The rate of population growth "+"City_name "+" in the year is "+str(pop_rate)+" %!")

Note: I modified the minimum possible.

UPDATE As mentioned in another reply, the return is also in trouble

Using fstring:

return(f"The rate of population growth {City_name} in the year is {str(pop_rate)}%!")

I hope it helps.

1

Hi, how are you? As Paulo said the for loop is wrong, in fact it does not need this repetition loop to do the assignment of the pop_rate variable, doing so:

pop_rate = pop_diff/year_diff * 100

I also noticed that in your return it is like this

return("The rate of population growth "+"City_name "+" in the year is "+str(pop_rate)+" %!")

Where the City_name parameter of your function is in quotes, in the case when leaving the function you will see instead of the name of the city "City_name"

Not to mention that as you put this message in the return of the function, to view it you should use the print method in it getting like this:

print(Grow_population_rate(parm1, param2, param3, param4, param5))

To remove the need of the print method, I suggest you put it inside the body of the function, and in the return put only the pop_rate that can be used in the future and will be hidden in the output, the code would be + or - like this:

def Grow_population_rate(year_1, year_2, City_name, pop_1, pop_2):
    pop_diff = pop_2 - pop_1
    year_diff = year_2 - year_1
    pop_rate = pop_diff/year_diff * 100:
    print("The rate of population growth "+ City_name +" in the year is "+str(pop_rate)+" %!") 
    return pop_rate

Browser other questions tagged

You are not signed in. Login or sign up in order to post.