Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. import random
  2. import time
  3. import sys
  4.  
  5. yorn = True #Boolean for restarting game, loops until it reaches a 'False' and 'Break'
  6.  
  7.  
  8. while yorn == True:
  9.  
  10. fourno = [0,1, 2, 3, 4, 5, 6, 7, 8, 9 ] #List used for number combination generation (stores as integers)
  11. random.shuffle(fourno) #Shuffle list
  12. fourno2 = fourno[::3] #Picks out the first 4 numbers in the list
  13. print (fourno2) #Outputs number for test to show it works
  14.  
  15. name = input("\nPlease enter your name: ")
  16. print ("Hello {0}!".format(name))
  17.  
  18. #Bull + Cow counter
  19.  
  20. bull = 0
  21. cow = 0
  22. while bull < 4: #Continue loop if the Bull counter is lower than 4
  23. bull = 0
  24. cow = 0
  25.  
  26. #User input
  27.  
  28.  
  29. ui = input("\nDo not enter duplicate numbers and only enter numbers in between 0-9.\n\nPlease type 'exit' if you would like to exit the game and reveal the numbers.\n\nPlease enter 4 digits: ").upper()
  30. if ui == "EXIT":
  31. print (fourno2)
  32. print ('\nExiting the script...\n')
  33. yorn == False
  34. break
  35.  
  36. #Divides the numbers into individual lists for comparison & Validation "Try & Except"
  37.  
  38. try:
  39. a = int(ui[0]) #Converted and then stored into integers
  40. b = int(ui[1])
  41. c =int(ui[2])
  42. d = int(ui[3])
  43.  
  44. except ValueError:
  45. print ("{0}\n Invalid input... ".format(ui))
  46. continue
  47.  
  48. print ("{0} {1} {2} {3}".format(a, b, c, d)) #Displays the numbers the user entered
  49.  
  50.  
  51. #Comparing the user input and the computer
  52.  
  53. #Comparing Bulls with Bulls
  54. #Adds value to Bull if a number is placed in the same position as the generated list
  55.  
  56. if a == fourno2[0]:
  57. bull = bull + 1
  58.  
  59. if b == fourno2[1]:
  60. bull = bull + 1
  61.  
  62. if c == fourno2[2]:
  63. bull = bull + 1
  64.  
  65. if d == fourno2[3]:
  66. bull = bull + 1
  67.  
  68.  
  69. #Comparing Cows with Cows, 1st set
  70. #Adds value to Cow if there is a correct number but is not placed in the same position/is placed in the same position
  71.  
  72. if a == fourno2[0]:
  73. cow = cow + 1
  74.  
  75. if a == fourno2[1]:
  76. cow = cow + 1
  77.  
  78. if a == fourno2[2]:
  79. cow = cow + 1
  80.  
  81. if a == fourno2[3]:
  82. cow = cow + 1
  83.  
  84.  
  85. #Comparing Cows with Cows, 2nd set
  86.  
  87. if b == fourno2[0]:
  88. cow = cow + 1
  89.  
  90. if b == fourno2[1]:
  91. cow = cow + 1
  92.  
  93. if b == fourno2[2]:
  94. cow = cow + 1
  95.  
  96. if b == fourno2[3]:
  97. cow = cow + 1
  98.  
  99.  
  100. #Comparing Cows with Cows, 3rd set
  101.  
  102. if c == fourno2[0]:
  103. cow = cow + 1
  104.  
  105. if c == fourno2[1]:
  106. cow = cow + 1
  107.  
  108. if c == fourno2[2]:
  109. cow = cow + 1
  110.  
  111. if c == fourno2[3]:
  112. cow = cow + 1
  113.  
  114.  
  115. #Comparing Cows with Cows, 4th set
  116.  
  117. if d == fourno2[0]:
  118. cow = cow + 1
  119.  
  120. if d == fourno2[1]:
  121. cow = cow + 1
  122.  
  123. if d == fourno2[2]:
  124. cow = cow + 1
  125.  
  126. if d == fourno2[3]:
  127. cow = cow + 1
  128.  
  129.  
  130. #Displays results
  131.  
  132. print ("Bull = {0}".format (bull))
  133. print ("Cow = {0} ".format (cow))
  134.  
  135.  
  136. #Play again?
  137.  
  138. restart = input("Would you like to play again?\nYes or No? (Can type 'Y' or 'N'): ").upper() #Avoiding long line of code with differently written answers
  139.  
  140. while not (restart == "Y" or restart == "N" or restart == "YES" or restart == "NO"): #Validating answer for restarting game
  141. print ("Not a valid answer\n")
  142. restart = input("Would you like to play again? ").upper()
  143.  
  144. if restart == "Y"or restart == "YES":
  145. print ("\nRestarting\n--------------------------------------------------------------------\n")
  146. yorn = True
  147. continue
  148.  
  149. elif restart == "N" or restart == "NO" :
  150. msg = ("\nThank you for playing!\nEnding program...\n..........\n..........\n..........\n\n\n\n")
  151. for i in msg:
  152. sys.stdout.write(i)
  153. sys.stdout.flush()
  154. time.sleep (0.05)
  155. yorn = False
  156. break
  157.  
  158. if ui == "exit":
  159. print (fourno2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement