傅元娴的作业一

代码


  f1 = open("作业一素材.txt", "r", encoding = "utf-8")

  def newlines():
      new_lines = []
      for line in f1.readlines():
          line = line.strip('.\n')
          new_lines.append(line)
      return new_lines
  result = newlines()
  #print(result)

  last_line = result[-3]
  #print(last_line)

  last_line_list = last_line.split(sep = " ")
  #print(last_line_list)

  last_word = last_line_list[-1]
  #print(last_word)

  def count():
      n = 0
      for i in last_word:
          n = n + 1
      return n
  length1 = count()

  print("最后一个单词的长度是:{}".format(length1))

结果

结果截图

解释(步骤)

附加题代码


  f2 = open("作业一附加选做题素材.txt", "r", encoding = "utf-8")

  def newlines2():
      new_lines2 = []
      for line in f2.readlines():
          line = line.strip('\n')
          new_lines2.append(line)
      return new_lines2
  result2 = newlines2()

  last_part1 = result2[-4].split("。")[1]
  last_part2 = result2[-3]
  last_sen = last_part1 + last_part2
  #print(last_sen)
  def count():
      n = 0
      for i in last_sen:
          n = n + 1
      return n
  length2 = count()
  print("最后一个句子的长度是:{}".format(length2))

结果

结果截图

附加题解释(步骤)