Average score - 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: Average score (/thread-2622.html) |
Average score - MartinEvtimov - Mar-29-2017 So I'm trying to start by asking the user how many scores they would like to enter. Then use a loop to request each score and add it to a total. Finally, calculate and display the average for the entered scores. Using separate subroutines/functions/methods. But I'm doing something wrong in the loopbody, can you help me? def scoretimes(): print("How many grade scores would you like to enter?") scores = int(input()) return scores def while_loop(scores): times = 0 while times <= scores: scoreinput = float(input()) scoretotal = scoretotal + scoreinput return scoretotal times += 1 def calculation(scores, scoretotal): calculation = scoretotal / scores return calculation def main(): scores = scoretimes() while_loop(scores) calculation(scores, scoretotal) main() Moderator Larz60+: Added code tags for you this time for future posts, please read:
RE: Average score - nilamo - Mar-29-2017 You return too early. Return after the while block, instead of inside of it. As it stands now, it'll return whatever the first score is, every time, without adding any of them together. RE: Average score - Larz60+ - Mar-29-2017 consider: def scoretimes(): scores = [] inp = input('Enter grade scores separated by space: ').split() for item in inp: scores.append(float(item)) scoretotal =sum(scores) return scores, scoretotal def calculate_average(scores, scoretotal): calculation = scoretotal / len(scores) return calculation def main(): scores, scoretotal = scoretimes() print('average grade score: {:.1f}'.format(calculate_average(scores, scoretotal))) main() RE: Average score - zivoni - Mar-29-2017 Maybe this little modification of Larz60+ code is easier to understand. His solution does exactly what you asked for (and how you tried to do it), but its probably easier to do it without adding to total. Just uses one function to "collect" scores and another one to do all computing. def get_scores(): scores = [] inp = input('Enter grade scores separated by space: ').split() for item in inp: scores.append(float(item)) return scores def calculate_average(scores): calculation = sum(scores) / len(scores) return calculation def main(): scores = get_scores() average = calculate_average(scores) print('average grade score: {:.1f}'.format(average)) main() RE: Average score - MartinEvtimov - Apr-02-2017 I'm almost there but I'm missing something can you tell me what? The error I get from repl.it is Traceback (most recent call last): File "python", line 30, in <module> File "python", line 25, in main File "python", line 10, in while_loop ValueError: could not convert string to float: def scoretimes(): print("How many grade scores would you like to enter?") scores = int(input()) return scores def while_loop(scores): times = 1 scoretotal = 0 while times <= scores: scoreinput = float(input("Enter score:")) scoretotal += scoreinput times += 1 return scoretotal def get_calculation(scores, scoretotal): calculation = scoretotal / scores return calculation def display_result(calculation): print("The Average score is:" + str(calculation)) def main(): scores = scoretimes() while_loop(scores) calculation = get_calculation(scoretotal, scores) display_result(calculation) main() RE: Average score - ichabod801 - Apr-02-2017 Please use python tags for your code. You were asked once already. The error you are getting is because you entered something that is not a floating point integer. It depends on what you entered. To avoid this you would check for correct input before/while you convert to float. There are several ways to do this, I'm not sure what you have covered so far. I would typically use try/except or sets. |