Python Forum
I am stuck on functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I am stuck on functions
#1
Hi all, I am working on a lab assignment and I am stuck...probably because it's friday and my brain stopped at 5pm. At any rate, here's the actual problem:
Create a Python program that does the following:

Each gymnast in a competition receives scores from 5 judges.
• Create a loop that loops 5 times
• Inside the loop, prompt the user to enter a score (from 0 to 10), and store it in a list
• After the loop, call a function called scoreCalculator, and pass the list to it
• The scoreCalculator function should calculate the average of the 5 scores, and return that average
• Display the average that you get back from the function like so

The average score is 8.637


My code is crap, but this is what I'm doing right now. I am beyond confused...any help would be greatly appreciated. I basically can't get the function to call the list and then return it as a print statement.

=====================================
def scoreCalculator(scores):
        total = scoreCalculator
        for score in scores:
            total = sum(scores)
    return score / 5


    scores = [0]
    count = 0

    while count < 5:
        number = input("Enter a score (0 - 10) -> ")
        scores.append(number)
        count += 1
Reply
#2
The way your indentations are set, the code after return will never get executed as they are on the
same indentation level. Remove the first indentation beginning with scores = [0] all the way to the
end.
Next, you never call your function.
After your while loop (beginning in col 0), add
scoreCalculator(scores)
Reply
#3
Hello!
It doesn't have to call sum() on each loop in the function. It doesn't need a loop at all. sum(list) will all the numbers in that list. Than divide the result to len(list).
Getting the scores... In Python when you want to loop certain times use the for loop. It is just for that.

for n in range(5):
    # do something
This will loop 5 times. You can use 'n' or not. In your case you don't need it.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
This is supposed to be a homework. We don't provide complete answers.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
Quote:This is supposed to be a homework. We don't provide complete answers.
True, when the person has made no effort to do the work.
In this case, all the code was there, only adjustment needed
Reply
#6
Yep. It is homework and i want to learn it. I have a degree in Language and Linguistics (which pays exactly zero) and I love that I can draw cognates, logically between basic language structure between verbal and programming. I just need some basic syntactical help, as Python (and Bash scripting) are so fresh and so mean mean.
Reply
#7
This reply:
Quote:This is supposed to be a homework. We don't provide complete answers.
Wasn't directed at you. It was directed at another user whose reply I deleted.
The user simply supplied a complete answer without any comment which is really not what we do here.

As Larz initially said, in your original post everything from line 8 on should not be indented.
Unindent that, and try passing the list you created to the function.

Post your current code and we will try to push you along to success.
Reply
#8
Awesome! I think the indentation is a result of me not knowing how to format posts via this interface. It is not that way in my code on Pycharm.
def scoreCalculator(scores):
    total = scoreCalculator
    for score in scores:
        total = sum(scores)
    return score / 5

scores = [0]
count = 0

while count < 5:
    number = input("Enter a score (0 - 10) -> ")
    scores.append(number)
    count += 1
    scoreCalculator(scores)
Reply
#9
K.  So scoreCalculator(scores) should not be in the while loop.  That needs to be unindented.

In your function, as Wavic said, if you are using the sum builtin you don't need the for loop. So total = sum(scores) would do it.
Also you want to divide total by the length of scores, not score.

Finally you need to convert your input to an int.
Assuming you don't need any input validation this will do it:
number = int(input("Enter a score (0 - 10) -> "))
Try to make these changes and post your code.

Also let us know if there is an error and how your program is behaving.
Reply
#10
Quote:In your function, as Wavic said, if you are using the sum builtin you don't need the for loop. So total = sum(scores) would do it.
Also you want to divide total by the length of scores, not score.

Yes, I wish I could just do that, but he wants us to be practicing loops and learn about functions. It's easy enough for me to think about analogous concepts that are similar to functions and loops, but there's a disconnect, with me, on making them work together.

ps. I really, really appreciate the attention and the help...all of you.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020