Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. def readScore():
  2. selection = input("What score would you like the read ? n 1 - Lowest score n 2 - Highest score n 3 - Average score... ")
  3. name = input("Please enter the name of the pupil...")
  4. classNo = input("Please enter the class of the pupil...")
  5.  
  6. myFile = sqlite3.connect("scores.db")
  7. c = myFile.cursor()
  8. c.execute('SELECT score FROM scores WHERE name = (?) AND classNo = (?)', (name, classNo,))
  9. row = c.fetchall()
  10. print(row)
  11.  
  12. try:
  13. if selection == "1":
  14. print (min(row))
  15. if selection == "2":
  16. print (max(row))
  17. if selection == "3":
  18. print (sum(row) /len(row))
  19. except:
  20. print("No scores for that student")
  21.  
  22. myFile.commit()
  23. myFile.close()
  24.  
  25. import random
  26. import os
  27. import time
  28. import sqlite3
  29. clear = lambda: os.system('cls')
  30.  
  31. if os.path.exists("scores.db") == False:
  32. myFile = sqlite3.connect("scores.db")
  33. c = myFile.cursor()
  34. c.execute('''CREATE TABLE scores
  35. (
  36. name TEXT,
  37. classNo INT,
  38. score INT
  39. )
  40. ''')
  41. myFile.commit()
  42. myFile.close()
  43.  
  44.  
  45. def scoreText(name, classNo, score):
  46. myFile = sqlite3.connect("scores.db")
  47. c = myFile.cursor()
  48.  
  49. c.execute('SELECT score FROM scores WHERE name = (?) AND classNo = (?)', (name, classNo,))
  50. scores = c.fetchall()
  51.  
  52. if len(scores) == 3:
  53. del scores[0]
  54. scores.append(score)
  55. scores = ', '.join(map(str, scores))
  56. print(scores)
  57. c.execute('UPDATE scores SET score = (?) WHERE name = (?) AND classNo = (?)', (scores, name, classNo,))
  58. else:
  59. scores = ', '.join(map(str, scores))
  60. print(scores)
  61. c.execute('INSERT OR REPLACE INTO scores(score, name, classNo) VALUES(?,?,?) ', (score, name, classNo,))
  62.  
  63. myFile.commit()
  64. myFile.close()
  65.  
  66. def test():
  67. score = 0
  68. count = 0
  69. name = input("Please enter your name before starting the test...")
  70. classNo = input("Please enter your class number before starting the test...")
  71. clear()
  72. while count < 10:
  73. num1 = random.randint(0,10)
  74. num2 = random.randint(0,10)
  75. symbol = random.randint(0,2)
  76.  
  77. if symbol == 0:
  78. answer = num1 + num2
  79. inAnswer = input("What is " + str(num1) + " + " + str(num2) + "?... ")
  80. clear()
  81. try:
  82. if int(inAnswer) == answer:
  83. print("Your answer was correct ! n")
  84. score = score + 1
  85. count = count + 1
  86. else:
  87. print("Your answer was incorrect n")
  88. count = count + 1
  89. except:
  90. print("Please enter a number - Try again")
  91. #time.sleep(1.5)
  92. clear()
  93.  
  94. if symbol == 1:
  95. answer = num1 - num2
  96. inAnswer = input("What is " + str(num1) + " - " + str(num2) + "?... ")
  97. clear()
  98. try:
  99. if int(inAnswer) == answer:
  100. print("Your answer was correct ! n")
  101. score = score + 1
  102. count = count + 1
  103. else:
  104. print("Your answer was incorrect n")
  105. count = count + 1
  106. except:
  107. print("Please enter a number - Try again")
  108. #time.sleep(1.5)
  109. clear()
  110.  
  111. if symbol == 2:
  112. answer = num1 * num2
  113. inAnswer = input("What is " + str(num1) + " X " + str(num2) + "?... ")
  114. clear()
  115. try:
  116. if int(inAnswer) == answer:
  117. print("Your answer was correct ! n")
  118. score = score + 1
  119. count = count + 1
  120. else:
  121. print("Your answer was incorrect n")
  122. count = count + 1
  123. except:
  124. print("Please enter a number - Try again")
  125.  
  126. #time.sleep(1.5)
  127. clear()
  128.  
  129. scoreText(name, classNo, score)
  130. print(name + " you have finished the test. Your score was..." + str(score))
  131.  
  132. time.sleep(2)
  133. clear()
  134. menu()
  135.  
  136.  
  137. def readScore():
  138. selection = input("What score would you like the read ? n 1 - Lowest score n 2 - Highest score n 3 - Average score... ")
  139. name = input("Please enter the name of the pupil...")
  140. classNo = input("Please enter the class of the pupil...")
  141.  
  142. myFile = sqlite3.connect("scores.db")
  143. c = myFile.cursor()
  144. c.execute('SELECT score FROM scores WHERE name = (?) AND classNo = (?)', (name, classNo,))
  145. row = c.fetchall()
  146. print(row)
  147.  
  148. try:
  149. if selection == "1":
  150. print (min(row))
  151. if selection == "2":
  152. print (max(row))
  153. if selection == "3":
  154. print (sum(row) /len(row))
  155. except:
  156. print("No scores for that student")
  157.  
  158. myFile.commit()
  159. myFile.close()
  160.  
  161. time.sleep(2)
  162. clear()
  163. menu()
  164.  
  165.  
  166. def ClassSort():
  167. myFile = sqlite3.connect("scores.db")
  168. c = myFile.cursor()
  169.  
  170. clear()
  171. classNo = input("Enter the class number you would like to sort... ")
  172. clear()
  173. type = input("How would you like to sort ? n 1 - Highest score, with students in alphabetical order n 2 - Highest score, highest to lowest n 3 - Average score, highest to lowest n... ")
  174.  
  175. if type == "1":
  176. c.execute('SELECT name, score FROM scores WHERE classNo = (?)', (classNo,))
  177. row = c.fetchall()
  178.  
  179. if type == "2":
  180. c.execute('SELECT name, score FROM scores WHERE classNo = (?)', (classNo,))
  181. row = c.fetchall()
  182.  
  183. if type == "3":
  184. c.execute('SELECT name, score FROM scores WHERE classNo = (?)', (classNo,))
  185. row = c.fetchall()
  186.  
  187. myFile.commit()
  188. myFile.close()
  189.  
  190.  
  191. def menu():
  192. selection = input("Press: n 1 - Run Test... n 2 - Read previous scores... n 3 - Sort class scores n... ")
  193. if selection == "1":
  194. test()
  195. if selection == "2":
  196. readScore()
  197. if selection == "3":
  198. ClassSort()
  199. menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement