Guest User

Untitled

a guest
Nov 1st, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.15 KB | None | 0 0
  1. print("Welcome to my quiz!")
  2. def maths():
  3. with open("maths.txt","r") as topic1:
  4. score = 0
  5. difficultyLevel = input("Please select a difficulty level for the maths quiz:easy, medium or hard:")
  6. questionsForMaths = topic1.readlines()
  7. print("The maths questions:")
  8. if difficultyLevel == "Easy" or difficultyLevel == "easy":
  9. for x in range(0,3):
  10. print(questionsForMaths[x].rstrip())
  11. userAnswer = input("Choose from the following:").lower()
  12. if userAnswer == questionsForMaths[1].rstrip():
  13. print ("correct")
  14. score = score + 1
  15. else:
  16. print ("incorrect")
  17. #2nd question
  18. for x in range(5,8):
  19. print(questionsForMaths[x].rstrip())
  20. userAnswer = input("Choose from the following:").lower()
  21. if userAnswer == questionsForMaths[7].rstrip():
  22. print ("correct")
  23. score = score + 1
  24. else:
  25. print ("incorrect")
  26. #3rd question
  27. for x in range(10,13):
  28. print(questionsForMaths[x].rstrip())
  29. userAnswer = input("Choose from the following:").lower()
  30. if userAnswer == questionsForMaths[12].rstrip():
  31. print ("correct")
  32. score = score + 1
  33. else:
  34. print ("incorrect")
  35. #4th question
  36. for x in range(15,18):
  37. print(questionsForMaths[x].rstrip())
  38. userAnswer = input("Choose from the following:").lower()
  39. if userAnswer == questionsForMaths[16].rstrip():
  40. print("correct")
  41. score = score + 1
  42. else:
  43. print("incorrect")
  44. #5th question
  45. for x in range(20,23):
  46. print(questionsForMaths[x].rstrip())
  47. userAnswer = input("Choose from the following:")
  48. if userAnswer == questionsForMaths[22].rstrip():
  49. print("correct")
  50. score = score + 1
  51. else:
  52. print("incorrect")
  53. elif difficultyLevel == "Medium" or difficultyLevel == "medium":
  54. for x in range(0,4):
  55. print(questionsForMaths[x].rstrip())
  56. userAnswer = input("Choose from the following:").lower()
  57. if userAnswer == questionsForMaths[1].rstrip():
  58. print ("correct")
  59. score = score + 1
  60. else:
  61. print ("incorrect")
  62. #2nd question
  63. for x in range(5,9):
  64. print(questionsForMaths[x].rstrip())
  65. userAnswer = input("Choose from the following:").lower()
  66. if userAnswer == questionsForMaths[7].rstrip():
  67. print ("correct")
  68. score = score + 1
  69. else:
  70. print ("incorrect")
  71. #3rd question
  72. for x in range(10,14):
  73. print(questionsForMaths[x].rstrip())
  74. userAnswer = input("Choose from the following:").lower()
  75. if userAnswer == questionsForMaths[12].rstrip():
  76. print ("correct")
  77. score = score + 1
  78. else:
  79. print ("incorrect")
  80. #4th question
  81. for x in range(15,19):
  82. print(questionsForMaths[x].rstrip())
  83. userAnswer = input("Choose from the following:").lower()
  84. if userAnswer == questionsForMaths[16].rstrip():
  85. print("correct")
  86. score = score + 1
  87. else:
  88. print("incorrect")
  89. #5th question
  90. for x in range(20,24):
  91. print(questionsForMaths[x].rstrip())
  92. userAnswer = input("Choose from the following:")
  93. if userAnswer == questionsForMaths[22].rstrip():
  94. print("correct")
  95. score = score + 1
  96. else:
  97. print("incorrect")
  98.  
  99. elif difficultyLevel == "Hard" or difficultyLevel == "hard":
  100. for x in range(0,5):
  101. print(questionsForMaths[x].rstrip())
  102. userAnswer = input("Choose from the following:").lower()
  103. if userAnswer == questionsForMaths[1].rstrip():
  104. print ("correct")
  105. score = score + 1
  106. else:
  107. print ("incorrect")
  108. #2nd question
  109. for x in range(5,10):
  110. print(questionsForMaths[x].rstrip())
  111. userAnswer = input("Choose from the following:").lower()
  112. if userAnswer == questionsForMaths[7].rstrip():
  113. print ("correct")
  114. score = score + 1
  115. else:
  116. print ("incorrect")
  117. #3rd question
  118. for x in range(10,15):
  119. print(questionsForMaths[x].rstrip())
  120. userAnswer = input("Choose from the following:").lower()
  121. if userAnswer == questionsForMaths[12].rstrip():
  122. print ("correct")
  123. score = score + 1
  124. else:
  125. print ("incorrect")
  126. #4th question
  127. for x in range(15,20):
  128. print(questionsForMaths[x].rstrip())
  129. userAnswer = input("Choose from the following:").lower()
  130. if userAnswer == questionsForMaths[16].rstrip():
  131. print("correct")
  132. score = score + 1
  133. else:
  134. print("incorrect")
  135. #5th question
  136. for x in range(20,25):
  137. print(questionsForMaths[x].rstrip())
  138. userAnswer = input("Choose from the following:")
  139. if userAnswer == questionsForMaths[22].rstrip():
  140. print("correct")
  141. score = score + 1
  142. else:
  143. print("incorrect")
  144. print("Your total score is", score)
  145. percentage = (score/5)*100
  146. print("The percentage of questions correct is", percentage)
  147. if percentage < 40.0:
  148. print("You have failed the maths quiz")
  149. with open("reports.txt","a") as reports:
  150. reports.write("n" + fullUsername + " " + "achieved a grade of fail" + " " + "in the maths quiz")
  151. elif percentage >= 40.0 and percentage <= 60.0:
  152. print("You have passed the maths quiz")
  153. with open("reports.txt","a") as reports:
  154. reports.write("n" + fullUsername + " " + "achieved a grade of pass" + " " + "in the maths quiz")
  155. elif percentage >= 60.0 and percentage <= 80.0:
  156. print("You have achieved merit for the maths quiz")
  157. with open("reports.txt","a") as reports:
  158. reports.write("n" + fullUsername + " " + "achieved a grade of merit" + " " + "in the maths quiz")
  159. elif percentage > 80.0:
  160. print("You have achieved distinction for maths quiz")
  161. with open("reports.txt","a") as reports:
  162. reports.write("n" + fullUsername + " " + "achieved a grade of distinction" + " " + "in the maths quiz")
  163. with open("reports.txt","a") as reports:
  164. reports.write("n" + fullUsername + " " + "did the maths quiz and" + " " + "achieved a score of" + " " + str(score))
  165. reports.write("n" + fullUsername + " " + "did the maths quiz and" + " " + "achieved a percentage of" + " " + str(percentage))
  166.  
  167.  
  168. def history():
  169. with open("history.txt","r") as topic2:
  170. score = 0
  171. print("The history questions:")
  172. difficultyLevel = input("Please select a difficulty level for the history quiz:easy, medium or hard:")
  173. questionsForHistory = topic2.readlines()
  174. if difficultyLevel == "Easy" or difficultyLevel == "easy":
  175. for y in range(0,3):
  176. print(questionsForHistory[y].rstrip())
  177. userAnswer2 = input("Choose from the following:")
  178. if userAnswer2 == questionsForHistory[2].rstrip():
  179. print("correct")
  180. score = score + 1
  181. else:
  182. print("incorrect")
  183. #2nd question
  184. for y in range(5,8):
  185. print(questionsForHistory[y].rstrip())
  186. userAnswer2 = input("Choose from the following:")
  187. if userAnswer2 == questionsForHistory[6].rstrip():
  188. print("correct")
  189. score = score + 1
  190. else:
  191. print("incorrect")
  192. #3rd question
  193. for y in range(10,13):
  194. print(questionsForHistory[y].rstrip())
  195. userAnswer2 = input("Choose from the following:")
  196. if userAnswer2 == questionsForHistory[12].rstrip() or userAnswer2 == questionsForHistory[12].rstrip().lower():
  197. print("correct")
  198. score = score + 1
  199. else:
  200. print("incorrect")
  201. #4th question
  202. for y in range(15,18):
  203. print(questionsForHistory[y].rstrip())
  204. userAnswer2 = input("Choose from the following:")
  205. if userAnswer2 == questionsForHistory[17].rstrip():
  206. print("correct")
  207. score = score + 1
  208. else:
  209. print("incorrect")
  210. #5th question
  211. for y in range(20,23):
  212. print(questionsForHistory[y].rstrip())
  213. userAnswer2 = input("Choose from the following:")
  214. if userAnswer2 == questionsForHistory[21].rstrip():
  215. print("correct")
  216. score = score + 1
  217. else:
  218. print("incorrect")
  219. elif difficultyLevel == "Medium" or difficultyLevel == "medium":
  220. for y in range(0,4):
  221. print(questionsForHistory[y].rstrip())
  222. userAnswer2 = input("Choose from the following:")
  223. if userAnswer2 == questionsForHistory[2].rstrip():
  224. print("correct")
  225. score = score + 1
  226. else:
  227. print("incorrect")
  228. #2nd question
  229. for y in range(5,9):
  230. print(questionsForHistory[y].rstrip())
  231. userAnswer2 = input("Choose from the following:")
  232. if userAnswer2 == questionsForHistory[6].rstrip():
  233. print("correct")
  234. score = score + 1
  235. else:
  236. print("incorrect")
  237. #3rd question
  238. for y in range(10,14):
  239. print(questionsForHistory[y].rstrip())
  240. userAnswer2 = input("Choose from the following:")
  241. if userAnswer2 == questionsForHistory[12].rstrip() or userAnswer2 == questionsForHistory[12].rstrip().lower():
  242. print("correct")
  243. score = score + 1
  244. else:
  245. print("incorrect")
  246. #4th question
  247. for y in range(15,19):
  248. print(questionsForHistory[y].rstrip())
  249. userAnswer2 = input("Choose from the following:")
  250. if userAnswer2 == questionsForHistory[17].rstrip():
  251. print("correct")
  252. score = score + 1
  253. else:
  254. print("incorrect")
  255. #5th question
  256. for y in range(20,24):
  257. print(questionsForHistory[y].rstrip())
  258. userAnswer2 = input("Choose from the following:")
  259. if userAnswer2 == questionsForHistory[21].rstrip():
  260. print("correct")
  261. score = score + 1
  262. else:
  263. print("incorrect")
  264. elif difficultyLevel == "Hard" or difficultyLevel == "hard":
  265. for y in range(0,5):
  266. print(questionsForHistory[y].rstrip())
  267. userAnswer2 = input("Choose from the following:")
  268. if userAnswer2 == questionsForHistory[2].rstrip():
  269. print("correct")
  270. score = score + 1
  271. else:
  272. print("incorrect")
  273. #2th question
  274. for y in range(5,10):
  275. print(questionsForHistory[y].rstrip())
  276. userAnswer2 = input("Choose from the following:")
  277. if userAnswer2 == questionsForHistory[6].rstrip():
  278. print("correct")
  279. score = score + 1
  280. else:
  281. print("incorrect")
  282. #3rd question
  283. for y in range(10,15):
  284. print(questionsForHistory[y].rstrip())
  285. userAnswer2 = input("Choose from the following:")
  286. if userAnswer2 == questionsForHistory[12].rstrip() or userAnswer2 == questionsForHistory[12].rstrip().lower():
  287. print("correct")
  288. score = score + 1
  289. else:
  290. print("incorrect")
  291. #4th question
  292. for y in range(15,20):
  293. print(questionsForHistory[y].rstrip())
  294. userAnswer2 = input("Choose from the following:")
  295. if userAnswer2 == questionsForHistory[17].rstrip():
  296. print("correct")
  297. score = score + 1
  298. else:
  299. print("incorrect")
  300. #5th question
  301. for y in range(20,25):
  302. print(questionsForHistory[y].rstrip())
  303. userAnswer2 = input("Choose from the following:")
  304. if userAnswer2 == questionsForHistory[21].rstrip():
  305. print("correct")
  306. score = score + 1
  307. else:
  308. print("incorrect")
  309. print("Your total score is", score)
  310. percentage = (score/5)*100
  311. print("The percentage of questions correct is", percentage)
  312. if percentage < 40.0:
  313. print("You have failed the history quiz")
  314. with open("reports.txt","a") as reports:
  315. reports.write("n" + fullUsername + " " + "achieved a grade of fail" + " " + "in the history quiz")
  316. elif percentage >= 40.0 and percentage <= 60.0:
  317. print("You have passed the history quiz")
  318. with open("reports.txt","a") as reports:
  319. reports.write("n" + fullUsername + " " + "achieved a grade of pass" + " " + "in the history quiz")
  320. elif percentage >= 60.0 and percentage <= 80.0:
  321. print("You have achieved merit for the history quiz")
  322. with open("reports.txt","a") as reports:
  323. reports.write("n" + fullUsername + " " + "achieved a grade of merit" + " " + "in the history quiz")
  324. elif percentage > 80.0:
  325. print("You have achieved distinction for history quiz")
  326. with open("reports.txt","a") as reports:
  327. reports.write("n" + fullUsername + " " + "achieved a grade of distinction" + " " + "in the history quiz")
  328. with open("reports.txt","a") as reports:
  329. reports.write("n" + fullUsername + " " + "did the history quiz and" + " " + "achieved a score of" + " " + str(score))
  330. reports.write("n" + fullUsername + " " + "did the history quiz and" + " " + "achieved a percentage of" + " " + str(percentage))
  331.  
  332.  
  333. with open("user account .txt","w") as userFile:
  334. usernamePart1 = input("Enter your name:")
  335. while not usernamePart1.isalpha():
  336. print("Invalid name, try again")
  337. usernamePart1 = input("Enter your name:")
  338. usernamePart2 = input("Enter your age:")
  339. while not usernamePart2.isdigit():
  340. print("try again")
  341. usernamePart2 = input("Enter your age:")
  342. fullUsername = usernamePart1[:3] + usernamePart2
  343. userFile.write("Username:" + fullUsername)
  344. with open("reports.txt","a") as reports:
  345. reports.write("n" + "Username:" + fullUsername)
  346. print(fullUsername)
  347.  
  348. UserYearGroup = int(input("Enter your year group:"))
  349. while UserYearGroup < 7 or UserYearGroup > 11:
  350. print("Invalid year group, enter again")
  351. UserYearGroup = int(input("Enter your year group:"))
  352. if UserYearGroup >= 7 and UserYearGroup <= 11:
  353. userFile.write("nYear Group:" + str(UserYearGroup))
  354. print(UserYearGroup)
  355.  
  356. password = input("Enter your password, it must be at least 6 characters long and no more than 11 characters:")
  357. while len(password) < 6 or len(password) > 11:
  358. print("Invalid password, try again:")
  359. password = input("Enter your password, it must be at least 6 characters long and no more than 11 characters:")
  360. if len(password) >= 6 and len(password) <= 11:
  361. userFile.write("nPassword:" + password)
  362.  
  363.  
  364.  
  365.  
  366. topicChoice = input("Which topic do you want to test yourself on : its either maths or history?")
  367. his = "history".lower()
  368. ma = "maths".lower()
  369.  
  370. if topicChoice == his:
  371. history()
  372.  
  373. elif topicChoice == ma:
  374. maths()
  375.  
  376. user_account_filename = "user account .txt"
  377. report_filename = "reports.txt"
  378.  
  379. expectedAnswers = {
  380. 'maths':[1,7,12,16,22],
  381. 'history':[2,6,12,17,21]
  382. }
  383.  
  384. level2questions = {
  385. 'easy':3,
  386. 'medium':4,
  387. 'hard':5
  388. }
  389.  
  390. question_num = 5
  391.  
  392. def start_test(topic, fullUsername):
  393.  
  394. score = 0
  395. questions_filename = topic + '.txt'
  396. questions = open(questions_filename).readlines()
  397. difficultyLevel = input("Please select a difficulty level for the " + topic + " quiz:easy, medium or hard:")
  398.  
  399. if difficultyLevel.lower in ["easy","medium","hard"]:
  400. difficultyLevel = difficultyLevel.lower()
  401. print("The " + topic + " questions:")
  402.  
  403. for i in range(question_num):
  404. for x in range(i * 5, i * 5 + level2questions[difficultyLevel]):
  405. print(questions[x].rstrip())
  406. userAnswer = input("Choose from the following:").lower()
  407.  
  408. if i == 2 and topic == "history":
  409. userAnswer2 = userAnswer2.lower()
  410. if userAnswer == questions[expectedAnswers[topic][i]].rstrip():
  411. print ("correct")
  412. score = score + 1
  413. else:
  414. print ("incorrect")
  415.  
  416. #TODO:else
  417.  
  418. print("Your total score is", score)
  419. test_result(score, topic, fullUsername)
  420.  
  421.  
  422. def test_result(score, topic, fullUsername):
  423. def test_result(result):
  424. report = "n" + fullUsername + " achieved a grade of " + result + " in the " + topic + " quiz"
  425. if result in ["merit", "distinction"]:
  426. result = "achieved " + result + " for"
  427. else:
  428. result += "ed"
  429. printout = "You have " + result + " the " + topic + " quiz"
  430. return printout, report
  431.  
  432. percentage = (score/5)*100
  433. print("The percentage of questions correct is", percentage)
  434. if percentage < 40.0:
  435. result = "fail"
  436. elif percentage >= 40.0 and percentage <= 60.0:
  437. result = "pass"
  438. elif percentage >= 60.0 and percentage <= 80.0:
  439. result = "merit"
  440. elif percentage > 80.0:
  441. result = "distinction"
  442.  
  443. printout, report = test_result(result)
  444. print(printout)
  445.  
  446. record_prefix = "n" + fullUsername + " did the " + topic + " quiz and achieved a "
  447. write_file(report_filename, report + record_prefix + "score of " + str(score) + record_prefix + "percentage of " + str(percentage))
  448.  
  449.  
  450.  
  451. def write_file(path,data):
  452. with open(path,'a+') as f:
  453. f.write(data)
  454.  
  455. def main():
  456. print("Welcome to my quiz!")
  457.  
  458. #fullUsername
  459. usernamePart1, usernamePart2 = "",""
  460. while not usernamePart1.isalpha():
  461. print("Invalid name, try again")
  462. usernamePart1 = input("Enter your name:")
  463. while not usernamePart2.isdigit():
  464. print("try again")
  465. usernamePart2 = input("Enter your age:")
  466. fullUsername = usernamePart1[:3] + usernamePart2
  467.  
  468. write_file(user_account_filename, "Username:" + fullUsername)
  469. write_file(report_filename, "nUsername:" + fullUsername)
  470.  
  471. print(fullUsername)
  472.  
  473. #UserYearGroup
  474. #I have modified some code here, add `UserYearGroup.isdigit()` and delete the `if UserYearGroup >= 7 and UserYearGroup <= 11`
  475. UserYearGroup = 0
  476. while UserYearGroup.isdigit() and not (7 <= int(UserYearGroup) <= 11):
  477. print("Invalid year group, enter again")
  478. UserYearGroup = input("Enter your year group:")
  479. write_file(user_account_filename, "nYear Group:" + UserYearGroup)
  480. print(UserYearGroup)
  481.  
  482.  
  483. #Password
  484. password = ""
  485. while not 6 <= len(password) <= 11:
  486. print("Invalid password, try again:")
  487. password = input("Enter your password, it must be at least 6 characters long and no more than 11 characters:")
  488. write_file(user_account_filename, "nPassword:" + password)
  489.  
  490. #TopicChoice
  491. topicChoice = input("Which topic do you want to test yourself on : its either maths or history?")
  492. #deleted `his` and `ma`
  493. if topicChoice.lower() in ["history","maths"]:
  494. start_test(topicChoice, fullUsername)
  495. #TODO:else
  496.  
  497.  
  498. if __name__ == "__main__":
  499. main()
Add Comment
Please, Sign In to add comment