需要有关功能分配的建议

时间:2018-09-18 00:28:15

标签: python

**感谢您在计算与分数平均值相对应的平均字母等级方面的所有帮助。我还需要列出每个年级和相应的字母年级。我对如何执行此操作有些困惑。我要打印其他功能吗?

def main():
    student_name = input('Please enter your first and last name: ')
    scores = askForScore()
    avg_score = calc_average(scores)
    letter_grade = determine_grade(avg_score)
    print(student_name)
    print('The average of 8 tests is', letter_grade)
    print('letter_grade\tnumber_grade')
    print('--------------------------')




def askForScore():
    score1 = float(input('Please enter the first test score:'))
    score2 = float(input('Please enter the second test score:'))
    score3 = float(input('Please enter the third test score:'))
    score4 = float(input('Please enter the fourth test score:'))
    score5 = float(input('Please enter the fifth test score:'))
    score6 = float(input('Please enter the sixth test score:'))
    score7 = float(input('Please enter the seventh test score:'))
    score8 = float(input('Please enter the eigth test score:'))
    return (score1, score2, score3, score4, score5, score6, score7, score8)



def calc_average(scores):
    avg_score = (scores[0] + scores[1] + scores[2] + scores[3] + scores[4] + scores[5] + scores[6] + scores[7]) / 8
    return avg_score



def determine_grade(avg_score):
   if avg_score >= 90 and avg_score <= 100:
       return 'A'
   elif avg_score >= 80 and avg_score <= 89:
       return 'B'
   elif avg_score >= 70 and avg_score <= 79:
       return 'C'
   elif avg_score >= 60 and avg_score <= 69:
       return 'D'
   else:
       return 'F'





main()   

2 个答案:

答案 0 :(得分:1)

您在这里。有一些问题。

1)从函数返回值时,需要将其分配给变量,以便可以将其传递给下一个函数。

2)字符串文字(如字母等级“ F”)必须放在单引号或双引号内。

def main():
    student_name = input('Please enter your first and last name: ')
    scores = askForScore()
    avg_score = calc_average(scores)
    letter_grade = determine_grade(avg_score)
    print(letter_grade)

def askForScore():
    score1 = float(input('Please enter the first test score:'))
    score2 = float(input('Please enter the second test score:'))
    score3 = float(input('Please enter the third test score:'))
    score4 = float(input('Please enter the fourth test score:'))
    score5 = float(input('Please enter the fifth test score:'))
    return (score1, score2, score3, score4, score5)



def calc_average(scores):
    avg_score = (scores[0] + scores[1] + scores[2] + scores[3] + scores[4]) / 5
    return avg_score



def determine_grade(avg_score):
   if avg_score >= 90 and avg_score <= 100:
       return 'A'
   elif avg_score >= 80 and avg_score <= 89:
       return 'B'
   elif avg_score >= 70 and avg_score <= 79:
       return 'C'
   elif avg_score >= 60 and avg_score <= 69:
       return 'D'
   else:
       return 'F'

main()

答案 1 :(得分:0)

确保您了解一些Python概念,例如变量的范围,返回语句和函数参数。例如,在您的情况下,score1无法score5内部的askForScore ... calc_average。实际上,calc_average返回您需要的值,并且这些值需要作为这样的参数传递给下一个函数:

...
score1, score2, score3, score4, score5 = askForScore()
calc_average(score1, score2, score3, score4, score5)
...