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

Python

Why isn't my counter working, it was working earlier on, when I ran a test, and now it isn't.

It is supposed to allow me to put in a number and then based on the number that I input, is how many times that I am supposed to enter a test score for a student. However it allows me to enter one number and then it asks for a test score input and then it goes to my endProgram statement. Any idears where I've messed up at?

def getNumber(): number = int(input("How many students took the test?: ")) return number

def getScores(counter, score, number, totalScores): for counter in range(0, number): score = input("Enter score for the student: ") totalScores = float(totalScores) + float(score) return totalScores

Is there more code that we're not seeing? If this is related to a challenge can you add the link to the challenge?

Also, please revise your question by using Markdown for the code. You can reference the Markdown Cheatsheet that's available underneath the text box if you're unfamiliar.

For python put enter you code like this:

```python

# Code Goes Here

```

Yes, there is more code, that I didn't include. The last post where I posted all of the code, came out confusing and no one has answered that query. No this isn't related to a challenge, it's related to a homework assignment for my online college class. Perhaps that isn't allowed, for me to ask those questions on this venue. If so, then I apologize, I'm still new to treehouse. No, I'm not familiar with the markdown cheatsheet. Are you referring to where you check to see if others have posted the same type of question? I don't really understand what the markdown cheatsheet is. I know when I posed the question, nothing else popped up as a previously similar post, that I could reference. Even though you've offered suggestions on a better way to repose my query, I'm afraid that I don't know where or how to access what you're referencing. I'm sorry, I will try to figure out what you're talking about and repose this question if it's allowable.

11 Answers

Thank you for posting all of your code. The problem is your getScores() function, your return statement is inside of your loop so it runs once then the function returns the totalScores.

Also, your loop in getScores() uses a range from 1 to "number", this means the number of scores will always be one less then you want. If you put in 5 students, it will ask you for only 4 scores because range starts at 1 and goes up to 5, but doesn't include 5. You can just use range(number) to get the right number of scores.

Here's my revision

def getScores(counter, score, number, totalScores):
    for counter in range(number):
        score = input("Enter score for the student: ")
        totalScores = float(totalScores) + float(score)
    return totalScores

You also have a syntax error when calling your printAverage function. You're assigning printAverage to averageScores instead of calling printAverage(averageScores).

printAverage(averageScores)

# Not

printAverage = (averageScores)

I hope this helps, you've coded a great program. I'll say that there are a lot of tweaks you can make to make it more readable and efficient. I highly recommend that as you continue learning you revisit this code to keep improving on it. Really great job!

I'm glad you helped me learn how to post all of my code properly, in order to ask about what I'm doing wrong. So, if my return in the getScores is in the loop and that is what's throwing that part off, then do I just skip a line to make it outside my loop? I'm going to fix the (number) issue and try that portion again. Thank you for the advice. I'm also going to try to fix the printAverage and see how that goes.

Thank you, for the encouragement. I'm really trying, Python has been very difficult for me to grasp. I'm getting better at it but when I run into a snag, it takes me a long time to figure out what I'm doing wrong before I can move on. It's really frustrating. That's why I joined treehouse. I had posted locally for a Python tutor but got no responses. It takes a while for my class instructor to answer me and then he's not very forth coming with whatever the issue is. I think he doesn't want it to be like he's just giving me the answers. If I have most of it but maybe it's a spacing issue or something a push in the right direction like, "hey, check your spacing", would do wonders and save me hours of frustration. Of course the next time I have a spacing issue, it will be easier to figure out. He doesn't really do that though. All my classes are online and so that makes things a little more difficult as well because my actual school is hours away from my house. None of our local colleges offer these classes. They only offer web design, and one other admin something or other (drawing a blank right now). Anyway, thank you for being willing to help.

'''Python def main():

endProgram, totalScores, counter, score, averageScores, number = declaredVariables()

while endProgram == "no":
    endProgram, totalScores, counter, score, averageScores, number = declaredVariables()

    number = getNumber()    
    totalScores = getScores(counter, score, number, totalScores)
    averageScores = getAverage(totalScores, number, averageScores)
    printAverage = (averageScores)

    endProgram = input("Do you want to end the program?(Enter no to process a new set of test scores.: ")

def declaredVariables(): endProgram = "no" totalScores = 0.0 counter = 1 score = 0.0 averageScores = 0.0 number = 0 return endProgram, totalScores, counter, score, averageScores, number

def getNumber(): number = int(input("How many students took the test?: ")) return number

def getScores(counter, score, number, totalScores): for counter in range(1, number): score = input("Enter score for the student: ") totalScores = float(totalScores) + float(score) return totalScores

def getAverage(totalScores, number, averageScores): averageScores = float(totalScores)/number return averageScores

def printAverage(averageScores): print("The average scores is: ", averageScores)

main() '''

It doesn't seem to have worked very well. I think that I followed the directions for the markdown by placing the three back ticks on the line above the code and then the line below the code. It didn't put all of the code in the code box though. Either I did it wrong or there is a limit on the amount of code that is allowed in the box. I'm not sure which is the case. So, I'm still not sure how to re-post this.

def main():

    endProgram, totalScores, counter, score, averageScores, number = declaredVariables()

    while endProgram == "no":
        endProgram, totalScores, counter, score, averageScores, number = declaredVariables()

        number = getNumber()    
        totalScores = getScores(counter, score, number, totalScores)
        averageScores = getAverage(totalScores, number, averageScores)
        printAverage = (averageScores)

        endProgram = input("Do you want to end the program?(Enter no to process a new set of test scores.: ")


def declaredVariables():
    endProgram = "no"
    totalScores = 0.0
    counter = 1
    score = 0.0
    averageScores = 0.0
    number = 0
    return endProgram, totalScores, counter, score, averageScores, number


def getNumber():
    number = int(input("How many students took the test?: "))
    return number

def getScores(counter, score, number, totalScores):
    for counter in range(1, number):
        score = input("Enter score for the student: ")
        totalScores = float(totalScores) + float(score)
        return totalScores

def getAverage(totalScores, number, averageScores):
    averageScores = float(totalScores)/number
    return averageScores

def printAverage(averageScores):
    print("The average scores is: ", averageScores)



main()

Ah ok, I must've picked the forward ticks rather than the backward ticks.

Ok, I guess I'm still not doing something right, I probably misunderstood you. I fixed the (1, number) to just read (number) and then I skipped a line for the return and then I added an = sign to the printAverage but python didn't like the = sign being there and so I had to take it out. The skipping a line for my return statement didn't seem to change anything, it still just allowed me to input one student's score even though I input that 9 students took the test. Then it asked me if I wanted to end the program.

Oh wow, I just noticed that in your advice, you have the return slightly to the left of where mine is. So, I tried that with mine and aha!, it didn't just ask me the first score it kept going. but I'm still on the print average issue. Thanks to your advice I'm getting there. little by little! So, if I want the return to be out of the loop, I need to have it tabbed to the left of the loop. Our book and the additional videos, don't explain that.

By the way, I'm absolutely positive that this could have been wrote in a more efficient way. I'm just stumbling though this Python. The raptor has been easier, of course.

notepad ++ from a previous class was easier as well. I think I've done a couple others but I can't even remember what they were now.

Oh My Gosh, I got it. You didn't want me to add an equals sign!!! See I thought that you were saying to take one out but I thought you were talking about at the bottom, and there wasn't one down there. So, then I thought you must've been telling me to add one rather than take one away. That wasn't it at all though. You were referring to the declared variables at the top, where there was an equals sign. Once I took that out, everything ran smoothly! Thank you so much!

Glad we're getting there. To print out the averages:

# Change this line
printAverage = (averageScores)

# to this
printAverage(averageScores)

Got it, thank you so much!