Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.06 KB | None | 0 0
  1. import random
  2. import csv
  3. import os
  4.  
  5. path = 'Language Data'
  6.  
  7. swap = False
  8.  
  9. fileChosen = -1
  10.  
  11. files, question, answer, value = [], [], [], []
  12.  
  13. chances, choices = [], []
  14.  
  15. answered = 0
  16. correct = 0
  17.  
  18. def giveData():
  19. with open(path + "/" + files[fileChosen], 'w', encoding = 'utf-8', newline = '') as file:
  20. writer = csv.writer(file, delimiter=',', quotechar='|')
  21. for i in range(0, len(value)):
  22. writer.writerow([question[i], answer[i], value[i]])
  23. sortData()
  24.  
  25. def sortData():
  26. with open(path + "/" + files[fileChosen], 'r', encoding = 'utf-8') as infile:
  27. reader = csv.reader(infile, delimiter=',')
  28. sort = sorted(reader, key = lambda row: int(row[2]), reverse = False)
  29. with open(path + "/" + files[fileChosen], 'w', encoding = 'utf-8', newline = '') as outfile:
  30. writer = csv.writer(outfile)
  31. for eachline in sort:
  32. writer.writerow(eachline)
  33. takeData()
  34.  
  35. def takeData():
  36. del question[:]
  37. del answer[:]
  38. del value[:]
  39. with open(path + "/" + files[fileChosen], 'r', encoding = 'utf-8') as file:
  40. reader = csv.reader(file, delimiter=',', quotechar='|')
  41. for row in reader:
  42. question.append(row[0])
  43. answer.append(row[1])
  44. value.append(int(row[2]))
  45.  
  46. def randNumber(amount):
  47. del choices[:]
  48. del chances[:]
  49. for i in range(0, amount):
  50. choices.append(i)
  51. chance = 1000
  52. while(len(chances) < amount):
  53. chances.append(chance)
  54. chance = chance - (chance / 10) - (chance / 10)
  55. return random.choices(choices, chances)[0]
  56.  
  57. def Input(message, info, data):
  58. message = message + " >> "
  59. userInput = input(message)
  60. if(info == "File"):
  61. if(userInput == "/C"):
  62. print("/C\n/View\n/Open <fileName>\n/New <fileName>\n/Remove <fileName>")
  63. return 0
  64. elif(userInput == "/View"):
  65. return 1
  66. elif(userInput[:5] == "/New "):
  67. return userInput
  68. elif(userInput[:8] == "/Remove "):
  69. return userInput
  70. elif(userInput[:6] == "/Open "):
  71. return userInput
  72. else:
  73. return "NIL"
  74. elif(info == "New Question" or info == "New Answer"):
  75. if(userInput == "/C"):
  76. print("/C\n/Back")
  77. return 0
  78. elif(userInput == "/Back"):
  79. return 1
  80. else:
  81. return userInput
  82. elif(info == "Remove Question"):
  83. if(userInput == "/C"):
  84. print("/C\n/Back")
  85. return -1
  86. elif(userInput == "/Back"):
  87. return -2
  88. elif(userInput.isdigit()):
  89. return int(userInput)
  90. else:
  91. return -3
  92. elif(info == "Action"):
  93. if(userInput == "/C"):
  94. print("/C\n/Back\n/Run\n/New\n/Remove\n/Remove All\n/View")
  95. return 0
  96. elif(userInput == "/Back"):
  97. return 1
  98. elif(userInput == "/Run"):
  99. return 2
  100. elif(userInput == "/New"):
  101. return 3
  102. elif(userInput == "/Remove"):
  103. return 4
  104. elif(userInput == "/Empty"):
  105. return 5
  106. elif(userInput == "/View"):
  107. return 6
  108. else:
  109. return 7
  110. elif(info == "Question"):
  111. if(userInput == "/C"):
  112. print("/C\n/Back\n/Swap\n/Reset")
  113. return 0
  114. elif(userInput == "/Back"):
  115. return 1
  116. elif(userInput == "/Swap"):
  117. return 2
  118. elif(userInput == "/Reset"):
  119. return 3
  120. else:
  121. return userInput
  122.  
  123. def FilePathCheck():
  124. del files[:]
  125. if not os.path.exists(path):
  126. os.makedirs(path)
  127. for filename in os.listdir(path):
  128. files.append(filename)
  129.  
  130. FilePathCheck()
  131.  
  132. print("Input /C to display commands")
  133. while(True):
  134. userInput = Input("File", "File", 0)
  135. if(userInput == 1):#View Files
  136. for i in range(0, len(files)):
  137. print(files[i][:-4])
  138. if(len(files) == 0):
  139. print("None")
  140. elif(userInput != 0):
  141. if(userInput[:8] == "/Remove "):#Remove File
  142. userInput = userInput[8:] + ".csv"
  143. for i in range(0, len(files)):
  144. if(userInput == files[i]):
  145. print("Done")
  146. os.remove(path + "/" + files[i])
  147. FilePathCheck()
  148. break
  149. elif(userInput[:6] == "/Open "):#Open File
  150. userInput = userInput[6:] + ".csv"
  151. fileChosen = -1
  152. for i in range(0, len(files)):
  153. if(userInput == files[i]):
  154. fileChosen = i
  155. takeData() if os.stat(path + "/" + files[fileChosen]).st_size != 0 else giveData()
  156. break
  157. if(fileChosen != -1):
  158. while(True):
  159. userInput = Input("Action", "Action", 0)
  160. if(userInput == 1):
  161. fileChosen = -1
  162. del question[:]
  163. del answer[:]
  164. del value[:]
  165. break
  166. elif(userInput == 2):
  167. while(True):
  168. if(len(value) > 0):
  169. num = randNumber(len(value))
  170. if(swap == False):
  171. userAnswer = Input(question[num], "Question", 0)
  172. else:
  173. userAnswer = Input(answer[num], "Question", 0)
  174. if(userAnswer == 1):
  175. break
  176. elif(userAnswer == 2):
  177. swap = (True if swap == False else False)
  178. elif(userAnswer == 3):
  179. answered = 0
  180. correct = 0
  181. elif((userAnswer == answer[num] and swap == False) or (userAnswer == question[num] and swap == True)):
  182. answered = answered + 1
  183. correct = correct + 1
  184. value[num] = value[num] + random.randint(1, 1)
  185. print(f"Correct! ({correct}/{answered})")
  186. giveData()
  187. elif(userAnswer != 0):
  188. answered = answered + 1
  189. value[num] = value[num] - random.randint(3, 3)
  190. print(f"Wrong! The answer is: {answer[num]} ({correct}/{answered})") if swap == False else print(f"Wrong! The answer is: {question[num]} ({correct}/{answered})")
  191. giveData()
  192. else:
  193. print("You need to /Create some questions")
  194. break
  195. elif(userInput == 3):
  196. while(True):
  197. while(True):
  198. userInput = Input("New Question", "New Question", 0)
  199. if(userInput == 1):
  200. break
  201. elif(userInput != 0):
  202. newQuestion = userInput
  203. break
  204. if(userInput == 1):
  205. break
  206. while(True):
  207. userInput = Input("New Answer", "New Answer", 0)
  208. if(userInput == 1):
  209. break
  210. elif(userInput != 0):
  211. newAnswer = userInput
  212. break
  213. if(userInput == 1):
  214. break
  215. question.append(newQuestion)
  216. answer.append(newAnswer)
  217. value.append(0)
  218. giveData()
  219. sortData()
  220. elif(userInput == 4):
  221. while(True):
  222. print("Which Question?")
  223. for i in range(0, len(value)):
  224. print(i, question[i], answer[i], value[i])
  225. print("")
  226. print(len(value), "Questions")
  227. userInput = Input("Question Number", "Remove Question", 0)
  228. if(userInput == -2):
  229. break
  230. elif(userInput != -3 and userInput != -1 and userInput > -1 and userInput < len(value)):
  231. del question[userInput]
  232. del answer[userInput]
  233. del value[userInput]
  234. giveData()
  235. break
  236. elif(userInput == 5):
  237. open(path + "/" + files[fileChosen], 'w').close()
  238. del question[:]
  239. del answer[:]
  240. del value[:]
  241. giveData()
  242. elif(userInput == 6):
  243. print("Questions", "Answers", "Values")
  244. for i in range(0, len(value)):
  245. print(question[i], answer[i], value[i])
  246. print("")
  247. print(len(value), "Questions")
  248. elif(userInput[:5] == "/New "):#New File
  249. userInput = userInput[5:]
  250. if(len(userInput) > 2):
  251. print("Done")
  252. newFile = path + "/" + userInput + ".csv"
  253. new = open(newFile, 'w')
  254. new.close()
  255. FilePathCheck()
  256. else:
  257. print("Longer Name")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement