Advertisement
Guest User

hmwkcode

a guest
Sep 6th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.66 KB | None | 0 0
  1. import sys
  2. from os.path import join
  3. import random
  4. import math
  5.  
  6. def triangle(score, username):
  7. count = 0
  8. length1 = random.randint(1,101)
  9. length2 = random.randint(2,101)
  10. areaTri = (length1 * length2) / 2
  11. areaTri = int(areaTri)
  12. wrong1 = areaTri + (random.randint(1,21))
  13. wrong2 = areaTri - (random.randint(1,21))
  14. while wrong2 < 0:
  15. wrong2 = areaTri - (random.randint(1,21))
  16. print("""
  17. The triangle has a length of {} and a width of {}""".format(length1, length2))
  18.  
  19. list1 = [wrong1, areaTri, wrong2]
  20. random.shuffle(list1)
  21. print1 = list1[0]
  22. print2 = list1[1]
  23. print3 = list1[2]
  24.  
  25.  
  26.  
  27. userSelect = int(input("""
  28. What is the area of the triangle?
  29. {}
  30. {}
  31. {}
  32. """.format(print1, print2, print3)))
  33.  
  34. if userSelect == areaTri:
  35. print("correct")
  36. score = score + 2
  37. retry = str(input("""do you want to continue?
  38. Y/N?
  39. """))
  40. while count < 1:
  41. if retry == "Y" or "y":
  42. triangle(score, username)
  43. count = count + 1
  44. elif retry == "N" or "n":
  45. print("your score was {}".format(score))
  46. count = count + 1
  47. mainShapeMenu(username)
  48. else:
  49. print("Sorry, that option is invalid")
  50. else:
  51. print("sorry, that was incorrect, try again")
  52. userSelect = int(input("""
  53. What is the area of the triangle?
  54. {}
  55. {}
  56. {}
  57. """.format(print1, print2, print3)))
  58. if userSelect == areaTri:
  59. print("correct")
  60. score = score + 1
  61. retry = str(input("""do you want to continue?
  62. Y/N?
  63. """))
  64. while count < 1:
  65. if retry == "Y" or "y":
  66. triangle(score, username)
  67. count = count + 1
  68. elif retry == "N" or "n":
  69. print("your score was {}".format(score))
  70. count = count + 1
  71. mainShapeMenu(username)
  72. else:
  73. print("Sorry, that option is invalid")
  74. else:
  75. print("Sorry, you answered incorrectly twice, the correct answer was {}, your final score was {}".format(areaTri, score))
  76. triCheck = open("{}triSave.txt".format(username), "r")
  77. triSave = open("{}triSave.txt".format(username), "w")
  78. score = str(score)
  79. triSave.write(score)
  80. mainShapeMenu(username)
  81.  
  82.  
  83.  
  84. def rectangle(score, username):
  85. count = 0
  86. length1 = random.randint(1,101)
  87. length2 = random.randint(2,101)
  88. areaRec = (length1 * length2)
  89. areaRec = int(areaRec)
  90. wrong1 = areaRec + (random.randint(1,21))
  91. wrong2 = areaRec - (random.randint(1,21))
  92. while wrong2 < 0:
  93. wrong2 = areaRec - (random.randint(1,21))
  94. print("""
  95. The rectangle has a length of {} and a width of {}""".format(length1, length2))
  96.  
  97. list1 = [wrong1, areaRec, wrong2]
  98. random.shuffle(list1)
  99. print1 = list1[0]
  100. print2 = list1[1]
  101. print3 = list1[2]
  102.  
  103. userSelect = int(input("""
  104. What is the area of the triangle?
  105. {}
  106. {}
  107. {}
  108. """.format(print1, print2, print3)))
  109.  
  110. if userSelect == areaRec:
  111. print("correct")
  112. score = score + 2
  113. retry = str(input("""do you want to continue?
  114. Y/N?
  115. """))
  116. while count < 1:
  117. if retry == "Y" or "y":
  118. rectangle(score, username)
  119. count = count + 1
  120. elif retry == "N" or "n":
  121. print("your score was {}".format(score))
  122. count = count + 1
  123. mainShapeMenu(username)
  124. else:
  125. print("Sorry, that option is invalid")
  126. else:
  127. print("sorry, that was incorrect, try again")
  128. userSelect = int(input("""
  129. What is the area of the rectangle?
  130. {}
  131. {}
  132. {}
  133. """.format(print1, print2, print3)))
  134. if userSelect == areaRec:
  135. print("correct")
  136. score = score + 1
  137. retry = str(input("""do you want to continue?
  138. Y/N?
  139. """))
  140. while count < 1:
  141. if retry == "Y" or "y":
  142. rectangle(score, username)
  143. count = count + 1
  144. elif retry == "N" or "n":
  145. print("your score was {}".format(score))
  146. count = count + 1
  147. mainShapeMenu(username)
  148. else:
  149. print("Sorry, that option is invalid")
  150. else:
  151. print("Sorry, you answered incorrectly twice, the correct answer was {}, your final score was {}".format(areaRec, score))
  152. recCheck = open("{}recSave.txt".format(username), "r")
  153. recSave = open("{}recSave.txt".format(username), "w")
  154. score = str(score)
  155. recSave.write(score)
  156. mainShapeMenu(username)
  157.  
  158. def circle(score, username):
  159. count = 0
  160. length1 = random.randint(1,101)
  161. areaCirc = (math.pi * length1)^2
  162. areaCirc = int(areaCirc)
  163. wrong1 = areaCirc + (random.randint(1,21))
  164. wrong2 = areaCirc - (random.randint(1,21))
  165. while wrong2 < 0:
  166. wrong2 = areaCirc - (random.randint(1,21))
  167. print("""
  168. The circle has a radius of {}""".format(length1))
  169.  
  170. randomSelect1 = random.randint(0,3)
  171. randomSelect2 = random.randint(0,2)
  172. list1 = [wrong1, areaCirc, wrong2]
  173. print1 = list1[randomSelect1]
  174. list.remove(print1)
  175. print2 = list1[randomSelect2]
  176. list.remove(print2)
  177. print3 = list1[0]
  178.  
  179.  
  180.  
  181. userSelect = int(input("""
  182. What is the area of the circle?
  183. {}
  184. {}
  185. {}
  186. """.format(print1, print2, print3)))
  187.  
  188. if userSelect == areaCirc:
  189. print("correct")
  190. score = score + 2
  191. retry = str(input("""do you want to continue?
  192. Y/N?
  193. """))
  194. while count < 1:
  195. if retry == "Y" or "y":
  196. circle(score, username)
  197. count = count + 1
  198. elif retry == "N" or "n":
  199. print("your score was {}".format(score))
  200. count = count + 1
  201. mainShapeMenu(username)
  202. else:
  203. print("Sorry, that option is invalid")
  204. else:
  205. print("sorry, that was incorrect, try again")
  206. userSelect = int(input("""
  207. What is the area of the circle?
  208. {}
  209. {}
  210. {}
  211. """.format(print1, print2, print3)))
  212. if userSelect == areaCirc:
  213. print("correct")
  214. score = score + 1
  215. retry = str(input("""do you want to continue?
  216. Y/N?
  217. """))
  218. while count < 1:
  219. if retry == "Y" or "y":
  220. circle(score, username)
  221. count = count + 1
  222. elif retry == "N" or "n":
  223. print("your score was {}".format(score))
  224. count = count + 1
  225. mainShapeMenu(username)
  226. else:
  227. print("Sorry, that option is invalid")
  228. else:
  229. print("Sorry, you answered incorrectly twice, the correct answer was {}, your final score was {}".format(areaCirc, score))
  230. circCheck = open("{}circSave.txt".format(username), "r")
  231. circSave = open("{}circSave.txt".format(username), "w")
  232. score = str(score)
  233. circSave.write(score)
  234. mainShapeMenu(username)
  235.  
  236.  
  237.  
  238. def login():
  239. usernameLocal = str(input("what is your username?"))
  240. saveFile = open("loginDetails{}.txt".format(usernameLocal))
  241. passwordLocal = str(input("What is your password? (this is case sensitive)"))
  242. saveFilePassword = saveFile.readlines()
  243. saveFilePassword2 = saveFilePassword[1]
  244. print(saveFilePassword)
  245. length = len(saveFilePassword)
  246. passLength = len(passwordLocal)
  247.  
  248. passAscii = []
  249. passAscii2 = []
  250. count = 0
  251.  
  252. for i in range(length):
  253. saveFileAscii = ord(saveFilePassword2[i])
  254. passAscii.append(saveFileAscii)
  255. for i in range(passLength):
  256. userPassAscii = ord(passwordLocal[i])
  257. passAscii2.append(userPassAscii)
  258.  
  259. for i in range(length):
  260. if passAscii[i] == passAscii2[i]:
  261. count = count + 1
  262. else:
  263. pass
  264.  
  265. if count == length:
  266. username = usernameLocal
  267. print("all is good")
  268. mainShapeMenu(username)
  269. else:
  270. print("sorry, that password doesn't match")
  271. login()
  272.  
  273. def registerSave(username,password):
  274. # filePath = join("C:", "Users", "Louis", "My Documents", "loginDetails.txt")
  275. usernameLocal = username
  276. savePath = ("loginDetails{}.txt".format(usernameLocal))
  277. registerLogin = open(savePath, "w")
  278. registerLogin.write("{}\n{}".format(usernameLocal, password))
  279. registerLogin.close()
  280.  
  281. def usernameCreate():
  282. username = str(input("what would you like you username to be? "))
  283. passwordCreate(username)
  284.  
  285. def passwordCreate(username):
  286. password = str(input("What would you like your password to be? "))
  287. passLen = len(password)
  288. passwordAscii = []
  289. for i in range(passLen):
  290. asciiCheck = password[i]
  291. passwordAscii.append(ord(asciiCheck))
  292. print(passwordAscii)
  293. count = 0
  294. if passLen < 8:
  295. print("sorry, your password is too short")
  296. passwordCreate(username)
  297. else:
  298. for i in range(passLen):
  299. passwordCheck = password[i]
  300. if passwordCheck.isupper() == True:
  301. count = count + 1
  302. else:
  303. pass
  304.  
  305. if count > 0:
  306. registerSave(username,password)
  307. fileCreate(username)
  308. mainShapeMenu(username)
  309. #insert save function
  310. else :
  311. print("sorry, your password contains no capitals")
  312. passwordCreate(username)
  313.  
  314. def fileCreate(username):
  315. save1 = open("{}circSave.txt".format(username), "w")
  316. save1.close
  317. save2 = open("{}recSave.txt".format(username), "w")
  318. save2.close
  319. save3 = open("{}triSave.txt".format(username), "w")
  320. save3.close
  321.  
  322.  
  323. def start():
  324. startRequest = int(input("""
  325. 1. Register
  326. 2. Login
  327. """))
  328.  
  329. if startRequest == 1:
  330. usernameCreate()
  331.  
  332. else:
  333. login()
  334.  
  335. def mainShapeMenu(username):
  336. shapeSelect = int(input("""
  337. What shape do you want to practise?
  338. 1. Triangle
  339. 2. Rectangle
  340. 3. Circle
  341. 4. Exit"""))
  342. if shapeSelect == 1 or "triangle" or "Triangle":
  343. score = 0
  344. triangle(score, username)
  345. elif shapeSelect == 2 or "rectangle" or "Rectangle":
  346. score = 0
  347. rectangle(score, username)
  348. elif shapeSelect == 3 or "circle" or "Circle":
  349. score = 0
  350. circle(score, username)
  351. elif shapeSelect == 4 or "exit":
  352. sys.exit
  353. else:
  354. print("Sorry, that is not an option")
  355. mainShapeMenu()
  356.  
  357. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement