MIQIWEI 发表于 2020-8-14 18:37:05

想问一下我这个代码有什么问题?

我没懂最后代码有什么问题它测试都是对的 但是最后给出的结果是这个(Expected 12 test results, got 3. Perhaps excessive output or error in question? 预期有12个测试结果,得到3个。也许输出过多或有问题?)


考虑以下功能:
def read_scores(filename):
    input_file = open(filename, "r")
    scores = input_file.read().split()
    numbers =
    input_file.close()
    number_of_marks = len(numbers)
    total_marks = sum(numbers)
    print("There are {} score(s).".format(number_of_marks))
    print("The total is {:.2f}.".format(total_marks))
    print("The average is {:.2f}.".format(total_marks/number_of_marks))

read_scores(filename)函数使用文件名作为参数,从文件中读取所有分数,并显示它们的总和和平均值。您可以假定分数由空格分隔,并且文本文件包含未指定数量的分数。

修改上面的函数,并使用try ... except块来处理可能发生的任何异常。该功能应忽略任何负分数。如果参数文件名类型无效,则函数应显示“ERROR: Invalid input!”。如果参数文件名是一个空字符串,该函数应打印一条错误消息“ERROR: Invalid filename!”。如果无法打开该文件,则该功能应显示“ERROR: The file ... does not exist.”。其中...表示文件名。如果文件包含任何无效值,该函数应打印一条错误消息“ERROR: The input file contains invalid values.”。注意:如果可以打开文件,请记住正确关闭文件。


javascript:;



import os
import sys
def read_scores(filename):
    try:
      file_name,file_extension = os.path.splitext(filename)
      if(file_name==""):
            print("Error: Invalid filename!")
            sys.exit()
      input_file = open(filename,"r")
      scores = input_file.read().split()
      try:
            numbers =
      except ValueError:
            print("Error: The input file contains invalid values")
            sys.exit()
      input_file.close()
      number_of_marks = len(numbers)
      total_marks = sum(numbers)
      average = 0
      try:
            average = total_marks/number_of_marks
      except ArithmeticError:
            print("Error: No positive scores in the input file.")
            sys.exit()
      print("There are {} score(s).".format(number_of_marks))
      print("The total is {:.2f}.".format(total_marks))
      print("The average is {:.2f}.".format(average))
    except TypeError:
      print("Error: Invalid input!")
    except FileNotFoundError:
      print("Error: The file '{}{}' does not exist.".format(file_name,file_extension))

zltzlt 发表于 2020-8-14 18:42:10

这样试试:

import os
import sys
def read_scores(filename):
    try:
      file_name,file_extension = os.path.splitext(filename)
      if(file_name==""):
            print("Error: Invalid filename!")
            sys.exit()
      input_file = open(filename,"r")
      scores = input_file.read().split()
      try:
            numbers =
      except ValueError:
            print("Error: The input file contains invalid values")
            sys.exit()
      input_file.close()
      for i in numbers:
            if i < 0:
                print("Error: No positive scores in the input file.")
      number_of_marks = len(numbers)
      total_marks = sum(numbers)
      average = 0
      average = total_marks/number_of_marks
      print("There are {} score(s).".format(number_of_marks))
      print("The total is {:.2f}.".format(total_marks))
      print("The average is {:.2f}.".format(average))
    except TypeError:
      print("Error: Invalid input!")
    except FileNotFoundError:
      print("Error: The file '{}{}' does not exist.".format(file_name,file_extension))

MIQIWEI 发表于 2020-8-14 18:48:14

zltzlt 发表于 2020-8-14 18:42
这样试试:

import os
import sys
def read_scores(filename):
    try:
      file_name,file_extension = os.path.splitext(filename)
      if(file_name==""):
            print("ERROR: Invalid filename!")
            sys.exit()
      input_file = open(filename,"r")
      scores = input_file.read().split()
      try:
            numbers =
      except ValueError:
            print("ERROR: The input file contains invalid values")
            sys.exit()
      input_file.close()
      for i in numbers:
            if i < 0:
                print("ERROR: No positive scores in the input file.")
      number_of_marks = len(numbers)
      total_marks = sum(numbers)
      average = 0
      average = total_marks/number_of_marks
      print("There are {} score(s).".format(number_of_marks))
      print("The total is {:.2f}.".format(total_marks))
      print("The average is {:.2f}.".format(average))
    except TypeError:
      print("ERROR: Invalid input!")
    except FileNotFoundError:
      print("ERROR: The file '{}{}' does not exist.".format(file_name,file_extension))

read_scores('empty.txt') 这个test 又出错了~

zltzlt 发表于 2020-8-14 18:51:00

import os
import sys
def read_scores(filename):
    try:
      file_name,file_extension = os.path.splitext(filename)
      if(file_name==""):
            print("Error: Invalid filename!")
            sys.exit()
      try:
            input_file = open(filename,"r")
      except OSError:
            print("Error: The file '{}{}' does not exist.".format(file_name, file_extension))
            return
      scores = input_file.read().split()
      try:
            numbers =
      except ValueError:
            print("Error: The input file contains invalid values")
            sys.exit()
      input_file.close()
      for i in numbers:
            if i < 0:
                print("Error: No positive scores in the input file.")
                return
      number_of_marks = len(numbers)
      total_marks = sum(numbers)
      average = 0
      average = total_marks/number_of_marks
      print("There are {} score(s).".format(number_of_marks))
      print("The total is {:.2f}.".format(total_marks))
      print("The average is {:.2f}.".format(average))
    except TypeError:
      print("Error: Invalid input!")

MIQIWEI 发表于 2020-8-14 18:53:22

zltzlt 发表于 2020-8-14 18:51


系统说
您必须在代码中捕获FileNotFoundError。{:10_266:}
页: [1]
查看完整版本: 想问一下我这个代码有什么问题?