鱼C论坛

 找回密码
 立即注册
查看: 910|回复: 1

[已解决]做题啦做题啦!!python

[复制链接]
发表于 2021-12-28 15:20:05 | 显示全部楼层 |阅读模式
60鱼币
1. 请编写一个类 Int,该类有一个成员属性 num,一个成员方法 getSum(),该方法计算属性 num 各个数位上的数字之和,返回指定输出格式的字符串对象,如输入“123”,输出为:“123 的各个数位上的数字之和为 6”,还有一个构造方法__init__(),用来传入一个 3 位正整数,初始化成员属性 num。请编写代码实现类 Int 并进行调用。

2. 如下代码生成一组学生的 Python 课程成绩,请编写函数 get_scores_info(scores),完成对 scores 列表中[0,60)、[60, 80)、[80, 90)、[90, 100]区间成绩的占比(百分比)的统计,并按照指定格式输出,输出格式形如:[0,60)占比为 15%。 import random scores = [random.randint(0,100) for i in range(200)]

3. 有如下内容:
#name,age,score tom,12,86
Lee,15,99
Lucy,11,58
Joseph,19,56
请编写代码完成以下功能:
1)将上述内容写入文件 e:\record.txt;
2)将文件中 score 低于 60 分记录的 name 输出;
3)计算文件中所有记录的 score 之和并输出。
最佳答案
2021-12-28 15:20:06
  1. # 1
  2. class Int:
  3.     def __init__(self, num):
  4.         self.num = num
  5.         
  6.     def getSum(self):
  7.         s = sum(map(int, self.num))
  8.         prompt = f"{self.num}的各个数位上的数字之和为{s}"
  9.         return prompt


  10. if __name__ == "__main__":
  11.     i = Int("123")
  12.     print(i.getSum())


  13. # 2
  14. import random

  15. scores = [random.randint(0,100) for i in range(200)]

  16. def get_scores_info(scores):
  17.     p0_60 = len([i for i in scores if i in range(0, 60)]) / len(scores)
  18.     p60_80 = len([i for i in scores if i in range(60, 80)]) / len(scores)
  19.     p80_90 = len([i for i in scores if i in range(80, 90)]) / len(scores)
  20.     p90_100 = len([i for i in scores if i in range(90, 101)]) / len(scores)
  21.    
  22.     kinds = ("[0, 60)", "[60, 80)", "[80, 90)", "[90, 100]")
  23.     nums = (p0_60, p60_80, p80_90, p90_100)
  24.     s_dict = dict(zip(kinds, nums))
  25.     for i in kinds:
  26.         print(f"{i}占比为{s_dict[i] * 100}%")


  27. if __name__ == "__main__":
  28.     get_scores_info(scores)


  29. # 3

  30. text = """name,age,score
  31. tom,12,86
  32. Lee,15,99
  33. Lucy,11,58
  34. Joseph,19,56"""

  35. with open(r"e:\record.txt", "w", encoding="utf-8") as f:
  36.     f.write(text)

  37. with open(r"e:\record.txt", "r", encoding="utf-8") as f:
  38.     data = f.read().splitlines()[1:]

  39. sum_score = 0
  40. for l in data:
  41.     r = l.split(",")
  42.     s = float(r[-1])
  43.     sum_score += s
  44.     if s < 60:
  45.         print(r[0])
  46. print(sum_score)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-12-28 15:20:06 | 显示全部楼层    本楼为最佳答案   
  1. # 1
  2. class Int:
  3.     def __init__(self, num):
  4.         self.num = num
  5.         
  6.     def getSum(self):
  7.         s = sum(map(int, self.num))
  8.         prompt = f"{self.num}的各个数位上的数字之和为{s}"
  9.         return prompt


  10. if __name__ == "__main__":
  11.     i = Int("123")
  12.     print(i.getSum())


  13. # 2
  14. import random

  15. scores = [random.randint(0,100) for i in range(200)]

  16. def get_scores_info(scores):
  17.     p0_60 = len([i for i in scores if i in range(0, 60)]) / len(scores)
  18.     p60_80 = len([i for i in scores if i in range(60, 80)]) / len(scores)
  19.     p80_90 = len([i for i in scores if i in range(80, 90)]) / len(scores)
  20.     p90_100 = len([i for i in scores if i in range(90, 101)]) / len(scores)
  21.    
  22.     kinds = ("[0, 60)", "[60, 80)", "[80, 90)", "[90, 100]")
  23.     nums = (p0_60, p60_80, p80_90, p90_100)
  24.     s_dict = dict(zip(kinds, nums))
  25.     for i in kinds:
  26.         print(f"{i}占比为{s_dict[i] * 100}%")


  27. if __name__ == "__main__":
  28.     get_scores_info(scores)


  29. # 3

  30. text = """name,age,score
  31. tom,12,86
  32. Lee,15,99
  33. Lucy,11,58
  34. Joseph,19,56"""

  35. with open(r"e:\record.txt", "w", encoding="utf-8") as f:
  36.     f.write(text)

  37. with open(r"e:\record.txt", "r", encoding="utf-8") as f:
  38.     data = f.read().splitlines()[1:]

  39. sum_score = 0
  40. for l in data:
  41.     r = l.split(",")
  42.     s = float(r[-1])
  43.     sum_score += s
  44.     if s < 60:
  45.         print(r[0])
  46. print(sum_score)
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
Cria + 5 + 5 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-1 05:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表