Python3-调用什么变量

时间:2018-09-28 05:35:55

标签: python-3.x function variables printing

在您批评我之前,我知道我是基础程序员,并且可能永远不会找到工作或变得很好。但是我需要帮助。你的帮助。我不知道要在函数中调用哪些变量来打印我的平均成绩和分数。下面提供的是我的代码,您介意谢谢吗。

def assigngrades(scores):
 avg = sum(scores)/len(scores)
 print(avg)


 for val in scores:
  if val > avg + 10:
   grade = 'A'
  elif val > avg + 5:
   grade = 'B'
  elif val > avg -5:
    grade = 'C'
  elif val > avg - 10:
   grade = 'D'
  else:
   grade = 'F'
  grades.append(grade)
 print("in assigngrades, grades: ",grades)
 return grades



def printsummary(grades):
 print('ID        Score        Average        Grade')
 print('===========================================')
 print( )


 for val in range(len(ids)):
  print('val', val)
  print(ids,' ',scores, ' ', grades)
 return

#main
ids = []
scores = []
grades = []
STOP = 0


id = int(input("Enter an ID:"))
while id > STOP:
 ids.append(id)
 score = eval(input("Enter a score:"))
 scores.append(score)
 id = (input("Enter an ID number, 0 to STOP:"))

 grades = assigngrades(scores)
 print("after while loop")
 print("Ids:", ids, "Scores:", scores, "Grades:", grades)
 printsummary(grades)

感谢您的帮助,我非常感谢您的宝贵时间。

1 个答案:

答案 0 :(得分:1)

您应该使assigngrades返回成绩和平均值作为元组,并使printsummary取平均值作为输出:

def assigngrades(scores):
    avg = sum(scores) / len(scores)
    for val in scores:
        if val > avg + 10:
            grade = 'A'
        elif val > avg + 5:
            grade = 'B'
        elif val > avg - 5:
            grade = 'C'
        elif val > avg - 10:
            grade = 'D'
        else:
            grade = 'F'
        grades.append(grade)
    return grades, avg

def printsummary(ids, scores, grades, avg):
    print('%7s%7s%7s' % ('ID', 'Score', 'Grade'))
    print('=' * 21)
    for id, score, grade in zip(ids, scores, grades):
        print('%7s%7s%7s' % (id, score, grade))
    print('Average: %f' % avg)

ids = []
scores = []
grades = []
STOP = 0

while True:
    id = int(input("Enter an ID:"))
    if id <= STOP:
        break
    ids.append(id)
    score = int(input("Enter a score:"))
    scores.append(score)
grades, avg = assigngrades(scores)
printsummary(ids, scores, grades, avg)

样本输入和输出:

Enter an ID:12
Enter a score:84
Enter an ID:13
Enter a score:95
Enter an ID:19
Enter a score:74
Enter an ID:0
     ID  Score  Grade
=====================
     12     84      C
     13     95      A
     19     74      F
Average: 84.333333