首页 > 解决方案 > 如何使用另一个函数的结果?

问题描述

我的代码从用户那里获取数字(等级)列表,并根据用户给出的数字找到平均值。找到平均值后,我想将平均值转换为基于字母的成绩。例如,90 的平均值将返回“A”,而 80 将返回“B”。

问题是我不能使用 calculated_average(x)函数的结果(平均值)并将其用于assign_grade().

有小费吗?

#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()

标签: python

解决方案


尝试执行以下代码。assign_grade功能在这里非常基本,但您可以根据需要对其进行编辑:

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)

输出(带输入 85):

print(final)
('A', 85.0)

推荐阅读