Advertisement
Guest User

Untitled

a guest
Feb 25th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.46 KB | None | 0 0
  1. 0
  2.  
  3.  
  4. I'm currently working on my OCR GCSE Program. I'm a new developer and this is my most extensive project yet.
  5.  
  6. The basis for the code is there, the program is working as intended, however my brief asks that the program run 5 times over for five separate rounds.
  7.  
  8. I'm struggling to understand while loops, whenever I try to implement a while loop. The program can not go past the login system.
  9.  
  10. I need some help figuring out how to loop this program and any suggestions on how to improve and optimise the code would be greatly appreciated! :) - Jayode18
  11.  
  12. import random
  13.  
  14. # Login system redone 19/11/18
  15.  
  16. user = ("user")
  17. passw = ("pass")
  18.  
  19. userName = input("Please enter your username: ")
  20. if userName == user:
  21.  
  22.  
  23. print("\n")
  24. print("Username correct!")
  25.  
  26. else:
  27. print("Username incorrect!")
  28. quit()
  29.  
  30. print("\n")
  31.  
  32. passWord = input("Now please enter your password: ")
  33. if passWord == passw:
  34. print("\n")
  35. print("User access granted! Game will now begin")
  36. print("\n")
  37.  
  38. else:
  39. print("Invalid Credentials")
  40. quit()
  41.  
  42. # Bug Update 19/11/18: Rewrote Login System. Bug fixed.
  43.  
  44. # Dice game rules / To Do list
  45. # - Dice total added to score. - DONE
  46. # - if total = even + 10 to score - DONE
  47. # - if total = odd - 5 to score - DONE
  48. # - if roll = double roll + 1 die and add roll to score - Simple if statement. Check if dice1 == dice2 and if yes then roll a third dice - DONE
  49. # - Score != < 0 - DONE
  50. # - Score after 5 rolls wins. - Learn while loops. IN PROGRESS
  51. # - if p1score == p2score roll 1 die and see who wins - Same as doubles. Just check scores after 5 rounds, and roll a third if need be. Repeat until win.
  52. # Save all scores at the end of every round and add to finalScorep1 & p2 variable. Compare these and whichever is higher, print winners name and highest roll.
  53.  
  54. # Ask both players for their names and store them locally to a file
  55.  
  56. p1Name = input("Player 1. Please enter your name: ")
  57. p2Name = input("Player 2. Please enter your name: ")
  58.  
  59. f=open("Player1_Data.txt" , "a")
  60. f.write("Player Name: " + p1Name + "\n")
  61. f.close()
  62.  
  63. f=open("Player2_Data.txt" , "a")
  64. f.write("Player Name: " + p2Name + "\n")
  65. f.close()
  66. print("\n")
  67.  
  68. # Ask if player 1 would like to roll their dice, and if yes, then roll them.
  69. player1Roll = input(p1Name + " would you like to roll your dice? Y/N: ")
  70. print("\n")
  71.  
  72.  
  73. # Credit to StackOveflow. (Finding User later) (Haha)
  74. if player1Roll ==("Y"):
  75. for x in range (1):
  76. print ("You rolled a:")
  77. Dice1 = int(random.randint(1,6))
  78. print(Dice1)
  79.  
  80. for x in range (1):
  81. print ("You rolled a:")
  82. Dice2 = int(random.randint(1,6))
  83. print(Dice2)
  84.  
  85.  
  86. diceTotalp1 = Dice1 + Dice2
  87.  
  88. score = diceTotalp1
  89.  
  90. oddScore = int(score) - 5
  91.  
  92. scoreZero = int(0)
  93.  
  94. evenScore = int(score) + 10
  95.  
  96. if score == int(2):
  97. print("You rolled an even number + 10 points!")
  98. score + 10
  99. score = evenScore
  100.  
  101. elif score == int(4):
  102. print("You rolled an even number + 10 points!")
  103. score + 10
  104. score = evenScore
  105.  
  106. elif score == int(6):
  107. print("You rolled an even number + 10 points!")
  108. score + 10
  109. score = evenScore
  110.  
  111. elif score == int(8):
  112. print("You rolled an even number + 10 points!")
  113. score + 10
  114. score = evenScore
  115.  
  116. elif score == int(10):
  117. print("You rolled an even number + 10 points!")
  118. score + 10
  119. score = evenScore
  120. elif score == int(12):
  121. print("You rolled an even number + 10 points!")
  122. score + 10
  123. score = evenScore
  124.  
  125. elif score == int(3):
  126. print("Aww. You rolled an odd number. - 5 points.")
  127. score - 5
  128. score = oddScore
  129.  
  130. elif score == int(5):
  131. print("Aww. You rolled an odd number. - 5 points.")
  132. score - 5
  133. score = oddScore
  134.  
  135.  
  136. elif score == int(7):
  137. print("Aww. You rolled an odd number. - 5 points.")
  138. score - 5
  139. score = oddScore
  140.  
  141. elif score == int(9):
  142. print("Aww. You rolled an odd number. - 5 points.")
  143. score - 5
  144. score = oddScore
  145.  
  146.  
  147. elif score == int(11):
  148. print("Aww. You rolled an odd number. - 5 points.")
  149. score - 5
  150. score = oddScore
  151.  
  152. elif score == int(0):
  153. print("Your score is already 0! It can't go any lower. That's just mean")
  154. score + 0
  155.  
  156.  
  157. if score == int(0):
  158. print("Your score is already 0! It can't go any lower. That's just mean")
  159. score + 0
  160.  
  161. # If Player 1 rolls double, roll a third dice and add it to their score
  162. if Dice1 == Dice2:
  163. print("\n")
  164. print("Congratulations! You rolled a double. Here's a bonus roll.")
  165. for x in range (1):
  166. print("You rolled a:")
  167. bonusDice = int(random.randint(1,6))
  168. print(bonusDice)
  169.  
  170. bonusScore = score + bonusDice
  171. score = bonusScore
  172.  
  173.  
  174.  
  175.  
  176. # Note to self: Remind that 2 other methods were attempted before settling on if/elif.
  177. # 1. Creating a variable that had all the even/odd numbers in them respectively, Outcome: Wouldn't work
  178. # 2. On launch, writing a list of all the even/odd numbers to a local file. And then reading that local file where appropriate.
  179. # Outcome: Could read and print the list, but could not read and apply them to an if statement.
  180.  
  181. # Shows the players what Player 1's final score for the round is
  182. print("\n")
  183. print(p1Name + "'s score for round 1 is: " + str (score))
  184. print("\n")
  185.  
  186. # Write Player 1's total for this round to a local file
  187. f=open("Player1_Data.txt" , "a")
  188. f.write("Round 1 Total Roll: " + str (diceTotalp1) + ("\n"))
  189. f.close()
  190.  
  191. # Write Player 1's final score for the round to a local file
  192. f=open("Player1_Data.txt" , "a")
  193. f.write("Round 1 Score: " + str (score) + ("\n"))
  194. f.close()
  195.  
  196. # Ask player 2 if they would like to roll, and if yes, then roll them.
  197. player2Roll = input(p2Name + " would you like to roll your dice? Y/N: ")
  198. print("\n")
  199. if player2Roll ==("Y"):
  200. for x in range (1):
  201. print ("You rolled a:")
  202. dice1 = int(random.randint(1,6))
  203. print(dice1)
  204.  
  205. for x in range (1):
  206. print ("You rolled a:")
  207. dice2 = int(random.randint(1,6))
  208. print(dice2)
  209.  
  210. diceTotalp2 = dice1 + dice2
  211.  
  212. scorep2 = diceTotalp2
  213.  
  214. oddScorep2 = int(scorep2) - 5
  215.  
  216. scoreZerop2 = int(0)
  217.  
  218. evenScorep2 = int(scorep2) + 10
  219.  
  220. if scorep2 == int(2):
  221. print("You rolled an even number + 10 points!")
  222. scorep2 + 10
  223. scorep2 = evenScorep2
  224.  
  225. elif scorep2 == int(4):
  226. print("You rolled an even number + 10 points!")
  227. scorep2 + 10
  228. scorep2 = evenScorep2
  229.  
  230. elif scorep2 == int(6):
  231. print("You rolled an even number + 10 points!")
  232. scorep2 + 10
  233. scorep2 = evenScorep2
  234.  
  235. elif scorep2 == int(8):
  236. print("You rolled an even number + 10 points!")
  237. scorep2 + 10
  238. scorep2 = evenScorep2
  239.  
  240. elif scorep2 == int(10):
  241. print("You rolled an even number + 10 points!")
  242. scorep2 + 10
  243. scorep2 = evenScorep2
  244. elif scorep2 == int(12):
  245. print("You rolled an even number + 10 points!")
  246. scorep2 + 10
  247. scorep2 = evenScorep2
  248.  
  249. elif scorep2 == int(3):
  250. print("Aww. You rolled an odd number. - 5 points.")
  251. scorep2 = oddScorep2
  252.  
  253. elif scorep2 == int(5):
  254. print("Aww. You rolled an odd number. - 5 points.")
  255. scorep2 = oddScorep2
  256.  
  257.  
  258. elif scorep2 == int(7):
  259. print("Aww. You rolled an odd number. - 5 points.")
  260. scorep2 = oddScorep2
  261.  
  262. elif scorep2 == int(9):
  263. print("Aww. You rolled an odd number. - 5 points.")
  264. scorep2 = oddScorep2
  265.  
  266.  
  267. elif scorep2 == int(11):
  268. print("Aww. You rolled an odd number. - 5 points.")
  269. scorep2 = oddScorep2
  270.  
  271. elif scorep2 == int(0):
  272. print("Your score is already 0! It can't go any lower. That's just mean")
  273. scorep2 + 0
  274.  
  275. # If Player 2 rolls a double, roll a bonus die and add it to their score.
  276. if dice1 == dice2:
  277. print("\n")
  278. print("Congratulations! You rolled a double. Here's a bonus roll.")
  279. for x in range (1):
  280. print("You rolled a:")
  281. bonusDicep2 = int(random.randint(1,6))
  282. print(bonusDicep2)
  283.  
  284. bonusScorep2 = scorep2 + bonusDicep2
  285. scorep2 = bonusScorep2
  286.  
  287. # Shows the players what Player 2's final score for the round is.
  288. print("\n")
  289. print(p2Name + "'s score for round 1 is: " + str (scorep2))
  290.  
  291. # Write Player 2's roll for this round to a local file
  292. f=open("Player2_Data.txt" , "a")
  293. f.write("Round 1 Total Roll: " + str(diceTotalp2)+ ("\n"))
  294. f.close()
  295.  
  296. f=open("Player2_Data.txt" , "a")
  297. f.write("Round 1 Total Score: " + str(scorep2))
  298. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement