these are her instructions:
- Write a class called TemperatureFile.
- The class has one data attribute: __filename
- Write getter and setter for the data attribute.
- Write a “calculateAverage” function (signature below) to calculate and
- return average temperature of all temperatures in the file.
- def calculateAverage(self):
- Handle possible exceptions
- Write a main function:
- Create a file ('Temperatures.txt’) and then use “write” method to write
- temperature values on each line.
- Create an object of the TemperaureFile with the filename
- (‘Temperatures.txt’) just created
- Use the object to call calcuateAverage function and print out the
- returned average temperature. Handle possible exceptions in main function
-
class TemperatureFile:
def __init__(self, filename):
self.__filename = filename
def getFilename(self):
return self._Filename
def setFilename(self):
self._filename = filename
def calculateAverage(self):
try:
for line in temperatureFile:
amount = float(line.rstrip("\n"))
total += amount
temp = temp + 1
average = total/temp
print(average)
except ValueError as err:
print(err)
except IOError as err:
print(err)
else:
average = total/temp
print(average)
finally:
temperatureFile.close()
def main():
num1 = 35
num2 = 63
num3 = 40
total = 0.0
temp = 0.0
average = 0.0
temperatureFile = open('Temperatures.txt', 'w')
temperatureFile.write(str(num1) + '\n')
temperatureFile.write(str(num2) + '\n')
temperatureFile.write(str(num3) + '\n')
temperatureFile.close()
Temperatures = TemperatureFile('Temperatures.txt')
temperatureFile = open('Temperatures.txt', 'r')
calculateAverage(temperatureFile)
main()
I keep receiving an error File "C:\Python27\temp.py", line 49, in main calculateAverage(temperatureFile) NameError: global name 'calculateAverage' is not defined
I'm wondering if i did this right at all or if i was on the right track and how to fix it