1

I'm trying to get the profitability of every project by dividing profit by revenue. The code is working, I get the values back.

I just need help with the last part (the dividing part). There is where I'm having some issues.

Here is my code.

The outcome I get is

AttributeError: 'str' object has no attribute 'append'

from observations.constants import PROJECTS_DB_ID
from datetime import datetime
from dateutil.relativedelta import relativedelta

def get(gs_client):
    #Sheet access
    sheet = gs_client.open_by_key(
        PROJECTS_DB_ID).worksheet('Finance')

    #Columns necessary
    projects = sheet.col_values(1)[2:]
    months = sheet.col_values(2)[2:]
    profit = sheet.col_values(11)[2:]
    revenue = sheet.col_values(6)[2:]
    last_modified = sheet.col_values(13)[2:]

    #Lists
    list_projects = []
    list_months = []
    list_profit = []
    list_revenue = []
    list_last_modified = []
    value = []

    #Gets each project
    for project in projects:
        list_projects.append(project)

    #Gets each month
    for month in months:
        list_months.append(month)

    #Gets each value of profit column
    for val in profit:
        list_profit.append(val.strip('$').replace(',',''))

    #Gets each value in revenue column
    for value in revenue:
        list_revenue.append(value.strip('$').replace(',',''))

    #Gets each date in last modified column
    for update in last_modified:
        list_last_modified.append(update)

    #Get profitability per project (profit divided by revenue)
    for x in range(len(projects)):
        value1 = float(list_profit[x])/float(list_revenue[x])
        value.append(value1)

    print(value)

Any help would be greatly appreciated!

6
  • 1
    Which particular append is failing? You have a few there.. Commented Jan 7, 2020 at 18:10
  • 3
    could you share at which line the error occurs? Commented Jan 7, 2020 at 18:10
  • Please edit to include the full traceback. You should also read the guidance for reducing your problem to a minimal reproducible example. Commented Jan 7, 2020 at 18:18
  • In your 4th for loop you're changing the type of value from list to str. Commented Jan 7, 2020 at 18:19
  • The error is in line 45 Commented Jan 7, 2020 at 18:27

1 Answer 1

3

Your error is due to variable value, you have used it as list and as string.

    #Lists
    list_projects = []
    list_months = []
    list_profit = []
    list_revenue = []
    list_last_modified = []
    value = []

    #Gets each project
    for project in projects:
        list_projects.append(project)

    #Gets each month
    for month in months:
        list_months.append(month)

    #Gets each value of profit column
    for val in profit:
        list_profit.append(val.strip('$').replace(',',''))

    #Gets each value in revenue column
    for val in revenue: # here, changed value to val
        list_revenue.append(val.strip('$').replace(',',''))

    #Gets each date in last modified column
    for update in last_modified:
        list_last_modified.append(update)

    #Get profitability per project (profit divided by revenue)
    for x in range(len(projects)):
        value1 = float(list_profit[x])/float(list_revenue[x])
        value.append(value1)

whenever you use for i in somthing in python, the i isn't local variable inside the for loop like in other language, value of i is the last value of i inside the loop, which can also be accessed after the end of the loop. You have to be very careful about the use of variable names in python.

Sign up to request clarification or add additional context in comments.

2 Comments

This worked, thank you so much. WIll be extra careful next time.
try to keep more descriptive variable names so you don't accidentally overwrite them.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.