Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Cheri Castro
226 PointsMy blood drive program isn't working. After the input of pints received, then I receive errors.
#Lab 10-3 Blood Drive
#the main function
def main():
endProgram = 'no'
while endProgram == 'no':
option = 0
print ('Enter 1 to enter in new data and store to file')
print ('Enter 2 to display data from the file')
option = int(input('Enter now ->'))
# declare variables
pints = [0] * 7
totalPints = 0
averagePints = 0
if option == 1:
# function calls
getPints(pints)
getTotal(pints, totalPints)
getAverage(totalPints, averagePints)
writeToFile(averagePints, pints)
else:
readFromFile(averagePints, pints)
endProgram = input('Do you want to end program? (Enter no or yes): ')
while not (endProgram == 'yes' or endProgram == 'no'):
print ('Please enter a yes or no')
endProgram = input('Do you want to end program? (Enter no or yes): ')
#the getPints function
def getPints(pints):
counter = 0
while counter < 7:
pints[counter] = input('Enter pints collected: ')
counter = counter + 1
return pints
#the getTotal function
def getTotal(pints, totalPints):
counter = 0
while counter < 7:
totalPints = int(totalPints) + int(pints[counter])
counter = counter + 1
return totalPints
#the getAverage function
def getAverage(totalPints, averagePints):
averagePints = float(totalPints / 7)
return averagePints
#the writeToFile function
def writeToFile(averagePints, pints):
outFile = open('blood.txt', 'a')
print >> outFileFile,'Pints Each Hour'
counter = 0
while counter < 7:
outFile.write(str(pints[counter]) + '\n')
counter = counter + 1
outFile.write('Average Pints')
outFile.write(str(averagePints) + '\n\n')
outFile.close()
#the readFromFile function
def readFromFile(averagePints, pints):
inFile = open('blood.txt', 'r')
str1 = inFile.read()
print (str1)
pints = inFile.read()
print (pints)
print #adds a blank line
averagePints = inFile.read()
print (averagePints)
inFile.close()
# calls main
main()
4 Answers
Thomas Nilsen
14,957 Pointsyour mistake is in this method:
def writeToFile(averagePints, pints):
outFile = open('blood.txt', 'a')
print >> outFileFile,'Pints Each Hour'
counter = 0
while counter < 7:
outFile.write(str(pints[counter]) + '\n')
counter = counter + 1
outFile.write('Average Pints')
outFile.write(str(averagePints) + '\n\n')
outFile.close()
specifically this line:
print >> outFileFile,'Pints Each Hour'
The correct name is outFile, and drop the >>, like this:
print outFile,'Pints Each Hour'
Cheri Castro
226 PointsOh boy, I don't know where that filefile came from. Perhaps, from all of my efforts, trying to get that specific portion not to result in an error. I have fixed a few errors but couldn't get past that one. I tried everything except for what you suggested, print>> outFile.'Pints Each Hour - print>> outFile, ('Pints Each Hour') - pints>> outFile ('Pints Each Hour'). It never occurred to me to try taking out the >>. This is a homework assignment and it was in the instructions that way. I haven't got very far in my Python on treehouse yet so, I still don't recognize all my mistakes yet. I'm learning though. I got through one whole Lab assignment without having to ask for any help, a couple days ago. Thank you for your input, I'm going to try it out.
Cheri Castro
226 PointsNope...that didn't fix it, first it stated that I needed parenthesis and then it told me that the outFile is invalid syntax.
Cheri Castro
226 PointsI think maybe I'm supposed to have a blood.txt somewhere but the instructions don't say anything about creating one. So, I'm really not sure.