Good Evening, I'm having some trouble with my code. My objective here is to input data into a class. So I have p1 = Pitch("CH", "S"). From here I want to run a function that prints a statement with this data entered with another function that finds the avg speed also inside the print statement. My bad if this isn't making to much sense but let me post my code that I have so far to see if it gives a better visual.
import csv
fh = open('pitches.csv')
spreadsheet = csv.DictReader(fh)
startspeed = []
class Pitch:
def __init__(self, name, result):
fh = open('pitches.csv')
self.spreadsheet = csv.DictReader(fh)
self.name = name
self.result = result
def avg_start_speed(self):
for row in spreadsheet:
if row['pitch_type'] == str(self.name) and row['type'] == str(self.result):
startspeed.append(row['start_speed'])
return sum(startspeed) / len(startspeed)
def myfunc(self):
for row in spreadsheet:
if row['pitch_type'] == self.name:
print("The average speed of a " + self.result + " for a "
+ self.name + " is " + str(self.avg_start_speed()))
p1 = Pitch("CH", "S")
p1.myfunc()
I am receiving this error
NameError: name 'avg_start_speed' is not defined
I'm not sure how to fix it. Thanks for all advice in advance.
CSV sample:
start_speed end_speed type pitch_type
92.9 84.1 S FF
92.8 84.1 S FF
94.1 85.2 S FF
91 84 B FF
75.4 69.6 B CU
92.9 84.8 S CH
93.3 85.3 B FF
89.3 82.4 X FC
92.1 85 S CH
self.avg_start_speed()instead.