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 PointsDoes anyone know why this won't run properly? The line with >> seems to be the problem.
#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 = int(float(totalPints / 7))
return averagePints
#the writeToFile function
def writeToFile(averagePints, pints):
outFile = open('blood.txt', 'a')
print>> outFile, '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()
I have tried a bunch of variations, such as:
print >> outFile, "Pints Each Hour"
print >> outFile, ('Pints Each Hour')
print >> outFile, ("Pints Each Hour')
print outFile, 'Pints each Hour'
print outFile, "Pints Each Hour"
print outFile, ('Pints Each Hour')
print outFile, ("Pints Each Hour")
print outFile. 'Pints Each Hour'
print outFile. "Pints Each Hour"
print outFile. ('Pints Each Hour')
print outFile. ("Pints Each Hour")
print outFile,. 'Pints Each Hour'
print outFile,. "Pints Each Hour"
and so on...the directions call for the line to be printed as it is in the main code area. It results in an error each time.
print>> outFile, 'Pints Each Hour'
TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and '_io.TextIOWrapper'. Did you mean "print(<message>, file=<output_stream>)"?
8 Answers
Steven Parker
243,318 PointsI'm not familiar with this use of "print", but that may explain it not working.
How about doing it the same way you do in the code further down:
outFile.write("Pints Each Hour")
Cheri Castro
226 PointsI don't think the write portion is supposed to be in there. I don't really understand why it is wrote in the instructions the way that it is, nor what the results are supposed to be, with it wrote that way. I do know that when you put write in there, it is supposed to write the input that a user types in, into a file, that can then be accessed and read later, if you choose a different option. Thank you for your input towards a solution.
Steven Parker
243,318 PointsThat's exactly what my suggestion will do, add that line to the file so you can read it back later.
Why do you think it should not be there?
Cheri Castro
226 PointsI just now saw your response here (below), about, why I don't think it should be there. Sorry, I had missed that. I am not sure that it's supposed to be there because, one the instructions call for it to be wrote a certain way and two because I see that it says it's supposed to write it to the file but other than that, I'm guessing that it is supposed to be a reactive print statement from the file. I don't know, I'm grasping it straws with this section.
Cheri Castro
226 PointsI don't have a link but I can copy and paste, the instructions come to us in a word document.
Lab 10.3 – File Access and Python Code The goal of this lab is to convert the blood drive program from Lab 10.1 to Python code.
Step 1: Start the IDLE Environment for Python. Prior to entering code, save your file by clicking on File and then Save. Select your location and save this file as Lab10-3.py. Be sure to include the .py extension.
Step 2: Document the first few lines of your program to include your name, the date, and a brief description of what the program does.
Step 3: Start your program with the following code:
Lab 10-3 Blood Drive
the main function
def main():
endProgram = 'no'
print
while endProgram == 'no':
option = 0
print
print 'Enter 1 to enter in new data and store to file'
print 'Enter 2 to display data from the file'
option = input('Enter now ->')
print
# declare variables
pints = [0] * 7
totalPints = 0
averagePints = 0
if option == 1:
# function calls
pints = getPints(pints)
totalPints = getTotal(pints, totalPints)
averagePints = getAverage(totalPints, averagePints)
else:
endProgram = raw_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 = raw_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 = totalPints + 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):
#the readFromFile function
def readFromFile(averagePints, pints):
# calls main
main()
Step 4: Under option 1 in main, add a function call to writeToFile and pass it averagePints and pints. This should be done after the other calls. This should look as follows:
writeToFile(averagePints, pints)
Step 5: Under option 2 in main, add a function call to readFromFile and pass it averagePints and pints. This should be done after the other calls. This should look as follows:
readFromFile(averagePints, pints)
Step 6: Under the documentation and the function header for the writeToFile function, create an outFile and call the open function. Pass this function the name of the text file and open it in append mode. This should look as follows:
outFile = open('blood.txt', 'a')
Step 7: The next step is to write the string ‘Pints Each Hour’ to the file. This is done as follows:
print >> outFile, 'Pints Each Hour'
Step 8: Initial counter to 0 and add a while loop with the condition of counter < 7. Inside the while loop, write the value of the array pints to the file. This should look as follows:
outFile.write(str(pints[counter]) + '\n')
counter = counter + 1
Step 9: Outside the while loop, write the string ‘Average Pints’ to the file.
Step 10: Next, write the averagePints variable to the file. This should look as follows:
outFile.write(str(averagePints) + '\n\n')
Step 11: The last item in this function is to close the outFile. This is done as follows:
outFile.close()
Step 12: Under the documentation and the function header for the readFromFile function, create an inFile and call the open function. Pass this function the name of the text file and open it in read mode. This should look as follows:
inFile = open('blood.txt', 'r')
Step 13: Next, read in the string ‘Pints Each Hour’ and print this to the screen. This is done as follows:
str1 = inFile.read()
print str1
Step 14: Read in the pints array as an entire list and print this to the screen. This is done as follows:
pints = inFile.read()
print pints
print #adds a blank line
Step 15: Read in the string ‘Average Pints’ and print this to the screen.
Step 16: Read in averagePints and print this to the screen.
Step 17: Close the inFile.
Step 18: Run your program and for the first execution, select option 1. Run the program more than once and enter at least 2 sets of data. The append mode should keep track of everything. The contents of your file will be stored in the same directory that your Python code is in. Paste the contents of the file below:
PASTE CONTENTS HERE
Step 19: Run your program again and select option 2 on the first iteration. This should display to the screen information that is stored in your file.
Step 20: Execute your program so that it works and paste the final code below
PASTE CODE HERE
Cheri Castro
226 PointsSome things, I've had to modify, such as where the raw_input is, I just have input. I guess the version I am using, of Python, is older or newer and so the raw_input doesn't work with my version. The other thing that I've had to modify is the lack of parenthesis around the print statements. My version won't print and will result in errors, if I don't put parenthesis. Also, in some areas where the result is supposed to be an integer, I have write it int(input(...)) I also often have to play with the tabs/spacing until I finally get something that will work, but it doesn't seem to be the same each time, I write a program.
Cheri Castro
226 PointsSeeing as the instructor never assigned Lab 9 to us and Lab 10-3,4,5 all reference a Lab 9. I think I've made some progress, surprisingly but still not quite there.
Steven Parker
243,318 PointsThe lack of parentheses around the argument to "print" clearly identify your course material as based on Python version 2 (old). The syntax shown for step 7 is also for the old version and no longer valid.
The line I already gave you can be used as the Python 3 equivalent.
Cheri Castro
226 PointsOh, well that would explain that then, I was thinking I had the older version. Ok, let me give it a go. Thank you for the explanation and for being so patient.
Cheri Castro
226 PointsHoly crap Batman, You're right! It worked. Well, mostly, my average came up to 0. So, I gotta figure out why the math didn't do anything but other than that, it worked! Thank you, thank you, thank you!