Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Average score
#1
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
Reply
#2
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.
Reply
#3
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()
Reply
#4
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()
Reply
#5
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()
Reply
#6
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to update score value? Mavoz 5 2,455 Nov-08-2022, 12:37 AM
Last Post: Mavoz

Forum Jump:

User Panel Messages

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