Guest User

Untitled

a guest
Jun 23rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. import random
  2. from time import sleep
  3. def welcome():
  4. sleep(1)
  5. print("This is how the game works: ")
  6. sleep(1)
  7. print("\t* A random 4 digit number is generated\n\t* You will be prompt to guess the number\n\t* Every time you guess a number which is in the mysterious number, you receive 1 bull.\n* Examples: \n\t\t* Let's say that the random number is 1234. If your guess is 1548, you will get 1 cow and 2 bulls because you managed to guess 2 numbers which are there ('1' and '4'). Additionally, you will get 1 cow because you guessed the right position of one of those numbers (number '1').\n\t\t* In a different example, where the random number stays 1234, if your guess is 1111, you will get 1 cow and 4 bulls. The reason for is that all the numbres you guessed are in the mysterious number, giving you 4 bulls. And because one of the '1' you guessed is in the right place, it gives you 1 cow.\n\t* Additionally, if you guess both rhe number and the place, you get 1 cow\n\t* If you guess no number correctly, you get nothing\n\t* Your aim should be to guess the number (and get 4 cows and 4 bulls) in as few attempts as possible")
  8. sleep(10)
  9. while True:
  10. q = str(input("Everything clear? Please answer either 'yes' or 'no': "))
  11. q = q.lower()
  12. if (q == "yes" or q == "y"):
  13. return print("Great! Let's get to it...\n")
  14. elif (q == "no" or q == "n"):
  15. return print("That's a shame... Maybe read the instructions again\n"), welcome(), sleep(1)
  16. else:
  17. print("Please try again. You mistyped the answer\n"), sleep(1)
  18.  
  19. def number():
  20. n = random.randint(1000, 9999)
  21. return n
  22.  
  23. def guess():
  24. print("Take a guess\n")
  25. while True:
  26. guess = int(input("Please enter a 4 digit number which doesn't start with '0': "))
  27. l = len(str(guess))
  28. if l != 4:
  29. return print("Please try again and this time, entere a 4 digit number.\n"), sleep(1)
  30. else:
  31. return guess
  32.  
  33. def game():
  34. print("Carry on guessing until you have 4 cows.")
  35. sleep(2)
  36. print("There is 1 in 8999 chance that you will guess the correct number on your first attempt. Let's see if today is your lucky day")
  37. sleep(3)
  38. cow = 0
  39. bull = 0
  40. count = 0
  41. attempts = 0
  42. ran_num_int = number()
  43. ran_num_str = str(ran_num_int)
  44. ran_num_list = list(ran_num_str[0:4])
  45. while cow != 4:
  46. input_num_int = guess()
  47. print("Your number is", input_num_int)
  48. input_num_str = str(input_num_int)
  49. input_num_list = list(input_num_str[0:4])
  50. sleep(1)
  51. a = ran_num_list
  52. b = input_num_list
  53. count = 0
  54. while count < 4:
  55. if a[count] == b[count]:
  56. cow = cow + 1
  57. count += 1
  58. for element in b:
  59. if element in a:
  60. bull = bull + 1
  61. print("Cow: ", cow,"; Bull: ", bull,"\n")
  62. if cow !=4:
  63. cow = 0
  64. bull = 0
  65. attempts = attempts + 1
  66. elif cow == 4:
  67. print("Well done")
  68. print("The random number was", ran_num_int)
  69. sleep(3)
  70. attempts = attempts + 1
  71. return print("It has taken you", attempts," attempts to guess the number")
  72.  
  73. print("Welcom to the game of 'Bulls & Cows'\n")
  74. welcome()
  75. print("Let's find out how many cows and bulls will this guess get you, shall we?\n")
  76. sleep(2)
  77. game()
Add Comment
Please, Sign In to add comment