1

My code gets a list of numbers (grades) from the user and finds the average based on the numbers the user has given. After finding the average, I want to turn the average into a letter based grade. For example, a 90 average would return "A" and 80 would return "B".

The problem is that I can't use the result (the average) from the calculated_average(x) function and use it at assign_grade().

Any tips?

#Gets a list of numbers from user
def get_score():
    score_list = []
    keep_going = 'y'
    while keep_going == 'y':
        score = float(input('Enter a test score: '))
        while score < 0:
            print('Positive numbers only')
            score = float(input('Enter a test score: '))
        score_list.append(score)
        keep_going = input("More scores (y/n) ")
    return score_list

#Calculates the average
def calculated_average(x):
    return sum(x) / len(x)

def assign_grade():





def main():
    score = get_score()
    print(calculated_average(score))

main()
Ralf
  • 16,086
  • 4
  • 44
  • 68
Xiao
  • 5
  • 4
  • 2
    What exactly is your problem? Why can't you use it? – Ralf Oct 05 '18 at 12:37
  • Why don't you save the returned result from `calculated_average(score)` in the same way that you saved the score from `get_score()`? – PM 2Ring Oct 05 '18 at 12:38
  • I can use it to find the average perfectly fine, but I want to turn the average into a letter grade based output instead. I can't seem to find a way to use the output from calculated_average and use it. – Xiao Oct 05 '18 at 12:39
  • https://stackoverflow.com/questions/1969240/mapping-a-range-of-values-to-another is a more general answer on how to map values. – Dschoni Oct 05 '18 at 12:44

2 Answers2

2

Try doing something like the code below. assign_grade function is very basic here, but you can edit it to your needs:

def get_score():
    score_list = []
    keep_going = 'y'
    while keep_going == 'y':
        score = float(input('Enter a test score: '))
        while score < 0:
            print('Positive numbers only')
            score = float(input('Enter a test score: '))
        score_list.append(score)
        keep_going = input("More scores (y/n) ")
    return score_list

#Calculates the average
def calculated_average(x):
    return sum(x) / len(x)

def assign_grade(x):
    if x>80:
        return 'A'
    else:
        return 'B'

def main():
    score = get_score()
    avg = calculated_average(score)
    letter = assign_grade(avg)
    return (letter, avg) 

final = main()
print(final)

Output (with input 85):

print(final)
('A', 85.0)
Piotrek
  • 1,400
  • 9
  • 16
1

well your code seems to work just you need to finalize the function assign_grade(x)

def assign_grade(x):
    if x>=90:
        return("A")
    elif 90>x>=80:
        return("B")
    else:
        return("C")
C.med
  • 581
  • 1
  • 5
  • 21