[关闭]
@chenwei123 2018-06-17T14:41:17.000000Z 字数 5491 阅读 523

课程加钻石&经验

冰橙公司


输入一节课钻石的最大值(maxZuan)、最小值(minZuan),经验的最大值(maxGrow)、最小值(minGrow)。指明 json 文件的读路径,写路径。就行了

Python3

  1. #!/usr/bin/env python3
  2. # -*-coding:utf-8 -*-
  3. #chmod a+x hello.py ,这样可以直接以./hello.py 的形式运行文件
  4. import json
  5. import time
  6. import codecs
  7. import logging
  8. import random
  9. #存储 json文件
  10. def store(data):
  11. with open('/Users/chen/Desktop/python3/data/pythonL52.json', 'wb') as json_file:
  12. json_file.write(json.dumps(data, sort_keys=True, indent=4, ensure_ascii=False).encode('utf8'))
  13. #读取 json 文件
  14. def openFile(filePath):
  15. with open(filePath, 'r') as f:
  16. data = json.load(f)
  17. return data
  18. #统计一节课中 action 的次数
  19. def countCourseAction(courseData):
  20. #判断最后一条消息是不是习题
  21. if "exercises" in courseData[-1]:
  22. #数组末尾追加一条打卡信息
  23. dic = {"record":True, "action":"打卡炫耀下", "message":"打卡炫耀下"}
  24. courseData.append(dic)
  25. else:
  26. courseData[-1]["record"] = True
  27. if "action" not in courseData[-1]:
  28. courseData[-1]["action"] = "打卡炫耀下"
  29. actionCount = 0
  30. for msg in courseData:
  31. #python2.7 has_key, python3.5 if key in dict
  32. if "exercises" in msg:
  33. # if msg.has_key('exercises'):
  34. wrongArray = msg["wrong"]
  35. for wrongMsg in wrongArray:
  36. if "action" in wrongMsg:
  37. actionCount +=1
  38. correctArray = msg["correct"]
  39. for correctMsg in correctArray:
  40. if "action" in correctMsg:
  41. actionCount +=1
  42. elif "action" in msg:
  43. actionCount +=1
  44. return actionCount
  45. #chapter,tag 编码,zuan_number,grow_number 赋值
  46. def operationCourseData(key, courseData, minZuan, maxZuan, minGrow, maxGrow):
  47. chapter = 1
  48. tag = 1
  49. for msg in courseData:
  50. #python2.7 has_key, python3.5 if key in dict
  51. zuanNum = random.randint(minZuan, maxZuan)
  52. growNum = random.randint(minGrow, maxGrow)
  53. if "exercises" in msg:
  54. if "tag" in msg:
  55. msg["tag"] = key +"-"+str(tag)
  56. tag += 1
  57. wrongArray = msg["wrong"]
  58. for wrongMsg in wrongArray:
  59. if "action" in wrongMsg:
  60. wrongMsg["chapter"] = chapter
  61. wrongMsg["zuan_number"] = 0
  62. wrongMsg["grow_number"] = 0
  63. chapter += 1
  64. correctArray = msg["correct"]
  65. i=0
  66. for correctMsg in correctArray:
  67. if "action" in correctMsg:
  68. correctMsg["chapter"] = chapter
  69. correctMsg["zuan_number"] = zuanNum
  70. correctMsg["grow_number"] = growNum
  71. chapter += 1
  72. i=i+1
  73. elif i==0 and "action" not in correctMsg:
  74. correctMsg["action"] = "下一条"
  75. correctMsg["chapter"] = chapter
  76. correctMsg["zuan_number"] = zuanNum
  77. correctMsg["grow_number"] = growNum
  78. chapter += 1
  79. i=i+1
  80. elif "action" in msg:
  81. if "record" in msg:
  82. msg["chapter"] = chapter
  83. msg["zuan_number"] = 0
  84. msg["grow_number"] = 0
  85. chapter += 1
  86. else:
  87. msg["chapter"] = chapter
  88. msg["zuan_number"] = zuanNum
  89. msg["grow_number"] = growNum
  90. chapter += 1
  91. if __name__ == "__main__":
  92. data = openFile('/Users/chen/Desktop/python3/data/pythonL5.json')
  93. # print(data)
  94. # maxZuan = input("输入一节钻石的最大数:")
  95. # minZuan = input("输入一节钻石的最小数:")
  96. # maxGrow = input("输入一节经验的最大数:")
  97. # minGrow = input("输入一节经验的最小数:")
  98. maxZuan = 50
  99. minZuan = 25
  100. maxGrow = 250
  101. minGrow = 130
  102. for key in data.keys():
  103. if key == "catalogs":
  104. continue
  105. print("第", key, "节")
  106. courseData = data[key] #节数据[]
  107. actionCount = countCourseAction(courseData)
  108. print("action 的次数:",actionCount)
  109. #添加钻石,经验
  110. minZu = int(int(minZuan)/actionCount)
  111. maxZu = int(int(maxZuan)/actionCount)
  112. minGr = int(int(minGrow)/actionCount)
  113. maxGr = int(int(maxGrow)/actionCount)
  114. print("钻石:", minZu, "~", maxZu)
  115. print("经验:", minGr, "~", maxGr)
  116. operationCourseData(key, courseData, minZu, maxZu, minGr, maxGr)
  117. # print(data)
  118. store(data)

Python2

  1. #!/usr/bin/env python
  2. # -*-coding:utf-8 -*-
  3. #chmod a+x hello.py ,这样可以直接以./hello.py 的形式运行文件
  4. import json
  5. import time
  6. import codecs
  7. import logging
  8. import random
  9. #存储 json文件
  10. def store(data):
  11. with open('/Users/chen/Desktop/python3/data/data1.json', 'wb') as json_file:
  12. json_file.write(json.dumps(data, sort_keys=True, indent=4, ensure_ascii=False).encode('utf8'))
  13. #读取 json 文件
  14. def openFile(filePath):
  15. with open(filePath, 'r') as f:
  16. data = json.load(f)
  17. return data
  18. #统计一节课中 action 的次数
  19. def countCourseAction(courseData):
  20. #判断最后一条消息是不是习题
  21. if "exercises" in courseData[-1]:
  22. #数组末尾追加一条打卡信息
  23. dic = {"record":True, "action":u"打卡炫耀下", "message":u"打卡炫耀下"}
  24. courseData.append(dic)
  25. else:
  26. courseData[-1]["record"] = True
  27. if "action" not in courseData[-1]:
  28. courseData[-1]["action"] = u"打卡炫耀下"
  29. actionCount = 0
  30. for msg in courseData:
  31. #python2.7 has_key, python3.5 if key in dict
  32. if "exercises" in msg:
  33. #if msg.has_key('exercises'):
  34. wrongArray = msg["wrong"]
  35. for wrongMsg in wrongArray:
  36. if "action" in wrongMsg:
  37. actionCount +=1
  38. correctArray = msg["correct"]
  39. for correctMsg in correctArray:
  40. if "action" in correctMsg:
  41. actionCount +=1
  42. elif "action" in msg:
  43. actionCount +=1
  44. return actionCount
  45. #chapter,tag 编码,zuan_number,grow_number 赋值
  46. def operationCourseData(key, courseData, minZuan, maxZuan, minGrow, maxGrow):
  47. chapter = 1
  48. tag = 1
  49. for msg in courseData:
  50. #python2.7 has_key, python3.5 if key in dict
  51. zuanNum = random.randint(minZuan, maxZuan)
  52. growNum = random.randint(minGrow, maxGrow)
  53. if "exercises" in msg:
  54. if "tag" in msg:
  55. msg["tag"] = key +"-"+str(tag)
  56. tag += 1
  57. wrongArray = msg["wrong"]
  58. for wrongMsg in wrongArray:
  59. if "action" in wrongMsg:
  60. wrongMsg["chapter"] = chapter
  61. wrongMsg["zuan_number"] = 0
  62. wrongMsg["grow_number"] = 0
  63. chapter += 1
  64. correctArray = msg["correct"]
  65. for correctMsg in correctArray:
  66. if "action" in correctMsg:
  67. correctMsg["chapter"] = chapter
  68. correctMsg["zuan_number"] = zuanNum
  69. correctMsg["grow_number"] = growNum
  70. chapter += 1
  71. elif "action" in msg:
  72. if "record" in msg:
  73. msg["chapter"] = chapter
  74. msg["zuan_number"] = 0
  75. msg["grow_number"] = 0
  76. chapter += 1
  77. else:
  78. msg["chapter"] = chapter
  79. msg["zuan_number"] = zuanNum
  80. msg["grow_number"] = growNum
  81. chapter += 1
  82. if __name__ == "__main__":
  83. data = openFile('/Users/chen/Desktop/python3/data/data.json')
  84. #print(data)
  85. #maxZuan = raw_input(u"输入一节钻石的最大数:")
  86. #minZuan = raw_input(u"输入一节钻石的最小数:")
  87. #maxGrow = raw_input(u"输入一节经验的最大数:")
  88. #minGrow = raw_input(u"输入一节经验的最小数:")
  89. maxZuan = 300
  90. minZuan = 200
  91. maxGrow = 500
  92. minGrow = 400
  93. for key in data.keys():
  94. if key == "catalogs":
  95. continue
  96. print u"第", key, u"节"
  97. courseData = data[key] #节数据[]
  98. actionCount = countCourseAction(courseData)
  99. print u"action 的次数:",actionCount
  100. #添加钻石,经验
  101. minZu = int(int(minZuan)/actionCount)
  102. maxZu = int(int(maxZuan)/actionCount)
  103. minGr = int(int(minGrow)/actionCount)
  104. maxGr = int(int(maxGrow)/actionCount)
  105. print u"钻石:", minZu, u"~", maxZu
  106. print u"经验:", minGr, u"~", maxGr
  107. operationCourseData(key, courseData, minZu, maxZu, minGr, maxGr)
  108. #print(data)
  109. store(data)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注