Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. def registration():
  2. ## asks for users username and password and then adds it to a list
  3. username = raw_input('Enter username ')
  4. password = raw_input('Enter password ')
  5. regis = []
  6. regis.append(username)
  7. regis.append (password)
  8. ##prints username and password and returns it to the main code
  9. print 'Username:', username
  10. print 'Password:', password
  11. return regis
  12.  
  13. def menu():
  14. ## prints different options for the user to choose from
  15. print ' --------- MENU --------- '
  16. print ''
  17. print ' Option 1: Rectangle'
  18. print ''
  19. print ' Option 2: Triangle'
  20. print ''
  21. print ' Option 3: Circle (pi = 3.14)'
  22. print ''
  23. print ' - Pick an option (number) - '
  24.  
  25.  
  26. def rectangle():
  27. ## if the user selects the rectangle this function will run
  28. import random
  29. ## selects random numbers between 1 and 12 for the width and height
  30. wid = random.randint(1,12)
  31. hei = random.randint(1,12)
  32. rarea = wid*hei
  33. ## creates different options for the user to choose from. Each vary a little
  34. ro1 = rarea
  35. r2 = rarea+1
  36. r3 = rarea-2
  37. r4 = rarea+4
  38. rectangleoptions = [ro1,r2,r3,r4]
  39. random.shuffle(rectangleoptions)
  40. ## asks the user to guess the area.
  41. print 'Width =', wid, ' Height =', hei
  42. print 'options are', rectangleoptions
  43. rguess = input('What is the area?')
  44. ## if they get it correct the first time, the user is rewarded 2 points
  45. if rguess == ro1:
  46. print 'correct'
  47. rscore = 2
  48. return rscore
  49. else:
  50. ## if they dont get it right the first time they are given a second chance after the user is the formula to help them
  51. print 'Try again'
  52. print 'The formula to find the area of a rectangle is width x height'
  53. rguess2 = input('What is the area?')
  54. if rguess2 == ro1:
  55. print 'correct'
  56. rscore = 1
  57. return rscore
  58. ## if they dont get it right they get zero points
  59. ## the score is returned to the main program
  60. else:
  61. print 'incorrect'
  62. print 'the answer was', ro1
  63. rscore = 0
  64. return rscore
  65.  
  66. def triangle():
  67. ## if the user selects the triangle this function will run
  68. import random
  69. wid1 = random.randint(1,12)
  70. hei1 = random.randint(1,12)
  71. tarea = 0.5 * wid1 * hei1
  72. to1 = tarea
  73. t2 = tarea+1
  74. t3 = tarea-2
  75. t4 = tarea+4
  76. triangleoptions = [to1,t2,t3,t4]
  77. random.shuffle(triangleoptions)
  78. print 'Width =', wid1, ' Height =', hei1
  79. print 'options are', triangleoptions
  80. tguess = input('What is the area? (type in exactly how option has shown (10.5))')
  81. if tguess == to1:
  82. print 'correct'
  83. tscore = 2
  84. return tscore
  85. else:
  86. print 'Try again'
  87. print 'The formula to find the area of a traingle is 1/2 x base x height'
  88. tguess2 = input('What is the area?')
  89. if tguess2 == to1:
  90. print 'correct'
  91. tscore = 1
  92. return tscore
  93. else:
  94. print 'incorrect'
  95. print 'the answer was', ro1
  96. tscore = 0
  97. return tscore
  98.  
  99. def storeinfile(stats):
  100. filename = 'stats.txt'
  101. f = open(filename, 'a')
  102. for i in stats:
  103. f.write(str(i) + "\n")
  104. f.close()
  105.  
  106. def circle():
  107. import random
  108. radius = random.randint(1,12)
  109.  
  110. carea = 3.14 * radius**2
  111. o1 = carea
  112. o2 = carea + 1
  113. o3 = carea - 2
  114. o4 = carea + 4
  115. circleoptions = [o1,o2,o3,o4]
  116. random.shuffle(circleoptions)
  117. print 'radius =', radius
  118. print 'options are', circleoptions
  119. cguess = input('What is the area? (type in exactly how option has shown (10.5))')
  120. if cguess == o1:
  121. print 'correct'
  122. cscore = 2
  123. return cscore
  124. else:
  125. print 'Try again'
  126. print 'The formula to find the area of a circle is pi x radius**2'
  127. cguess2 = input('What is the area?')
  128. if cguess2 == o1:
  129. print 'correct'
  130. cscore = 1
  131. return cscore
  132. else:
  133. print 'incorrect'
  134. print 'the answer was', o1
  135. cscore = 0
  136. return cscore
  137.  
  138.  
  139.  
  140.  
  141. try:
  142. regis = registration()
  143.  
  144. a = 1
  145. import sys
  146. total = 0
  147. while a == 1:
  148. menu()
  149. print ''
  150. option = raw_input('Choose an option ')
  151. if option == '1':
  152. rscore = rectangle()
  153. total = total + rscore
  154. elif option == '2':
  155. tscore = triangle()
  156. total = total + tscore
  157. elif option == '3':
  158. cscore = circle()
  159. total = total + cscore
  160. restart = raw_input('Do you want to choose another shape? y / n ')
  161. if restart == 'y':
  162. print ''
  163. elif restart == 'n':
  164. 'quitting program'
  165. a = 2
  166. print total
  167. stats = []
  168. stats.append(regis)
  169. stats.append(total)
  170. print stats
  171. storeinfile(stats)
  172. break
  173. else:
  174. print ''
  175.  
  176.  
  177.  
  178. except:
  179. print 'An error has occured'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement