I am stuck on functions - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: I am stuck on functions (/thread-6212.html) Pages:
1
2
|
I am stuck on functions - Hebruiser - Nov-11-2017 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 RE: I am stuck on functions - Larz60+ - Nov-11-2017 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) RE: I am stuck on functions - wavic - Nov-11-2017 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 somethingThis will loop 5 times. You can use 'n' or not. In your case you don't need it. RE: I am stuck on functions - wavic - Nov-11-2017 This is supposed to be a homework. We don't provide complete answers. RE: I am stuck on functions - Larz60+ - Nov-11-2017 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 RE: I am stuck on functions - Hebruiser - Nov-11-2017 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. RE: I am stuck on functions - Mekire - Nov-11-2017 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. RE: I am stuck on functions - Hebruiser - Nov-11-2017 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) RE: I am stuck on functions - Mekire - Nov-11-2017 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. RE: I am stuck on functions - Hebruiser - Nov-11-2017 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. 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. |