Robert Grootjen
Robert Grootjen

Reputation: 197

Why does my script return "AttributeError: 'str' object has no attribute 'append'?

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!

Upvotes: 1

Views: 1889

Answers (1)

Atreyagaurav
Atreyagaurav

Reputation: 1195

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.

Upvotes: 3

Related Questions