【问题标题】:Test Average and Grade with two additional functions使用两个附加功能测试平均和评分
【发布时间】:2014-10-16 02:09:40
【问题描述】:

我已经有一段时间了,我被困住了。我必须编写一个程序,询问用户每个分数和平均测试分数。我需要接受五个测试分数并返回分数平均值的函数 calcAverage。确定等级,它接受分数并根据标准评分等级将等级作为字符串返回(90-100 是 A,80-89 是 B,70-79 是 C,等等)。但我还需要一个 getValidScore 函数,它提示并读取一个有效的测试分数,一旦输入分数,它就会返回到调用模块。最后是 isInValidScore 是一个函数,它检查通过的测试分数是否在 0-100 范围内,并返回一个布尔值,让我知道它是否有效。 这是我所拥有的:

def main():

   test1 = int(input('Enter test grade 1: '))
   test2 = int(input('Enter test grade 2: '))
   test3 = int(input('Enter test grade 3: '))
   test4 = int(input('Enter test grade 4: '))
   test5 = int(input('Enter test grade 5: '))

   grade = (test1 + test2 + test3 + test4 + test5)

   input('press enter to continue')
   print(calc_average)
   print(determine_grade)
   print(getValidScore)
   print(isInvalidScore)
   return;

def calcAverage():

   grade / 5
   return averageScore

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

def getValidScore():
   grade = int(input('input score'))isInvalidScore(score)
   while isInvalidScore == false:
       print('ERROR')
   grade = int(input('input correct score'))
   return score

def isInvalidScore(score):
   status = True
   if score < 0 or score > 100:
      status = True
   else: status = False
   return status

main() 

所以我添加了回报,当我运行程序时,我得到了这个:

进入测试等级1:99

进入测试等级2:88

进入测试等级3:77

进入测试等级4:66

进入测试等级5:55

按回车键继续

函数 calc_average 在 0x02BAD228>

函数确定等级在 0x02BAD270>

函数 getValidScore 在 0x02BAD2B8>

function isInvalidScore at 0x02BAD300>

【问题讨论】:

    标签: python function average


    【解决方案1】:

    这里有一些问题:

    1. 您正在打印由内存中的位置表示的函数本身,而不是执行结果。在调用的末尾添加括号以打印结果。
    2. 您应该调用getValidScore 并返回一个整数结果,而不是在没有验证的情况下读取输入(请参阅main)。
    3. 您的一些函数接受参数,但您没有传递任何参数(提示:determinGradeisInvalidScore)。
    4. 您的某些函数应该接受参数(提示:calcAverage)。
    5. calcAverage 应该返回一个带有实际平均值的实数。 (提示:return grade/5
    6. 您的while 循环应以while isInvalidScore(grade) 开头,从而无需在上一行进行调用。
    7. main 中,您正在调用一个未定义的函数(determine_grade,是不是打错字了?)

    这就是我快速浏览的全部内容。祝你好运!

    【讨论】:

      【解决方案2】:

      @tdlive aw'sum 的答案实际上非常好(我刚刚投了赞成票)。阅读他的 cmets 并使用这些东西。我将为您提供一个更正的实现。我希望您研究您不了解的方面,但从中删除一些好的部分。祝你好运。

      isValidScore = lambda n: 0 <= n <= 100
      
      calcAverage = lambda *args: sum(args)/len(args)
      
      determinGrade = lambda s: ['F', 'D', 'C', 'B', 'A'][max(0, (min(60, 99)/10)-5)]
      
      def getValidScore():
         while True:
              try:
                  if isValidScore(int(raw_input('Input score: '))):
                      return score              
              except ValueError:
                  pass
              print('Score has to be an integer in the range 0 to 100')
      
      if __name__ == "__main__":
          scores = []
          for _ in range(5):
              scores.append(getValidScore())
          print('\nThe scores are: ' + str(scores))
          print('The average is: ' + str(calcAverage(*scores)))
          print('The grade is: ' + determinGrade(calcAverage(*scores)))
      

      【讨论】:

      • @Andy,你认为我需要一步一步解释代码还是这样就足够了?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-12
      相关资源
      最近更新 更多