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 trialCheri Castro
226 PointsMy average function doesn't seem to be working. Please, help.
#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')
outFile.write('Pints Each Hour')
counter = 0
while counter < 7:
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)
str2 = inFile.read()
print (str2)
print #adds a blank line
averagePints = inFile.read()
print (averagePints)
inFile.close()
# calls main
main()
I have gone back to the pseudocode and tried a variety of changes in the average statement, such as:
averagePints = int(totalPints)/7
averagePints =int(totalPints/7)
averagePints =int(totalPints)/int(7)
averagePints =float(totalPints/7)
averagePints =float(totalPints)/7
averagePints = totalPints/7
So, my results are looking like this:
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
RESTART: C:\Users\0wner\Desktop\Programming logic\Python Labs\Lab 10-3 test.py
Enter 1 to enter in new data and store to file
Enter 2 to display data from the file
Enter now ->1
Enter pints collected: 43
Enter pints collected: 25
Enter pints collected: 64
Enter pints collected: 35
Enter pints collected: 19
Enter pints collected: 37
Enter pints collected: 46
Do you want to end program? (Enter no or yes): no
Enter 1 to enter in new data and store to file
Enter 2 to display data from the file
Enter now ->2
Pints Each Hour43
25
64
35
19
37
46
Average Pints0
Pints Each Hour43
25
64
35
19
37
46
Average Pints0
Pints Each Hour43
25
64
35
19
37
46
Average Pints0
Pints Each Hour43
25
64
35
19
37
46
Average Pints0
Pints Each Hour43
25
64
35
19
37
46
Average Pints0
Pints Each HourAverage Pints0
Pints Each HourAverage Pints0
Pints Each HourAverage Pints0
Pints Each HourAverage Pints0
Pints Each HourAverage Pints0
Pints Each HourAverage Pints0
Pints Each HourAverage Pints0
Pints Each HourAverage Pints0
Do you want to end program? (Enter no or yes):
So, I don't think the Pints each Hour and Average Pints, are supposed to print on the same line nor without a space between them. Any ideas, on what else I have done wrong, would be greatly appreciated.
1 Answer
Cheri Castro
226 PointsNever mind, I finally got it! Just in case anyone else needs to know. I don't really have an explanation for why some of the things I tried worked, because my book for my class, doesn't explain what all of these things that we're being told to use mean. Also, because I haven't gotten far enough in the Python classes on here. So, I probably haven't covered that material yet, on here. However, all that being said, here's what finally worked. Oh, I was missing a whole line of code, that was some of the issue. Another issue was that I hadn't wrote my calls correctly, I suppose. Someone else had told me that it didn't matter, that I could write them either way, but I guess in the version of Python that I have, it does matter. I modified some other things, just so it would print the results in a way that was easier to read, in my opinion. So, at long last, here it is:
#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
pints = getPints(pints)
totalPints =getTotal(pints, totalPints)
averagePints = 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')
outFile.write('Pints Each Hour: '+'\n')
counter = 0
while counter < 7:
outFile.write(str(pints[counter])+'\n')
counter = counter + 1
outFile.write('Average Pints: '+'\n')
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()
And here are the results:
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
RESTART: C:\Users\0wner\Desktop\Programming logic\Python Labs\Lab 10-3 test.py
Enter 1 to enter in new data and store to file
Enter 2 to display data from the file
Enter now ->1
Enter pints collected: 34
Enter pints collected: 39
Enter pints collected: 25
Enter pints collected: 18
Enter pints collected: 43
Enter pints collected: 31
Enter pints collected: 12
Do you want to end program? (Enter no or yes): no
Enter 1 to enter in new data and store to file
Enter 2 to display data from the file
Enter now ->2
Pints Each Hour:
34
39
25
18
43
31
12
Average Pints:
28.857142857142858
Do you want to end program? (Enter no or yes): yes
>>>
So, fabulous! And all kinds of excited!!! Thank you to anyone who has helped out with this, from all the way back to a few days ago, even!