Defining variable

python

#1

I am doing an coding exercise using Python and obtain values from a csv.file. My questions on the assignment is to:
“Calculate the average change in “Profit/Losses” between months over the entire period.”

I know in my head to get this answer mathematically the formula would be something like “Average change in profit_loss = (Amount of last row - Amount of first row) / (Total length - 1)”

I’m getting an error message on not defining the variables. If I used last row and first row as my variable. What would it be equal too? I’m using the values from this csv file.


#2

Here’s a silly example of working with csv files


class Dog(Animal):
    def __init__(self):
        Animal.__init__(self)
        self.breed = ""
        self.age = 0
        self.weight = 0

    def readStats(self):
        filename = "Dog.csv"
        fields = [] #stores each data field
        with open(filename,'r') as csvfile: #use pythons csvreader
            csvreader = csv.reader(csvfile) #give file to reader
            next(csvreader) #ignore the first line (names of fields)
            fields = next(csvreader) #push all data into list
            self.name = fields[0]
            self.breed = fields[1]
            self.age = int(fields[2])
            self.color = fields[3]
            self.weight = int(fields[4])

    def loseTenLbs(self):
        self.weight -= 10

The concept is the same. Read the data from a csv file into your programmer defined variables.
Then you can operate on those variables


#3

Well I have read the csv.file into my python and I see my fields. But I’m lost on defining variables. For example. I had a question which asked “Calculate the total months in the dataset?” So I defined a variable as “totalNetAmount = 0”, I wrote my script as “total_months = total_months + 1”. I printed my script and it came back with my value
I’m just not sure when I’m trying to calculate value in the same row, how do I define the variables i will use in my script


#4

I’m confused. You just said how to do it

Is there something wrong about that?

And, is csv related to what you’re asking, or could that be cut out of the question entirely?


#5

I called back to cancel the ticket as I found the answer.

Thanks