Advertisement
Guest User

Untitled

a guest
May 27th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. # !python3
  2. # This is a small test/game which asks the user to enter a four digit number.
  3. # The number is then compared with a previously computer generated number -
  4. # the players goal is to guess the computer generated number correctly.
  5. # For each attempt the player sees the result with a "cow" and "bull" count.
  6. # legend:
  7. # cow = a user number matched exactly at the position within the random four digit number (4 cows = win)
  8. # bull = a user number can be found within the digit number list, but at a different position
  9.  
  10. import re
  11. from random import randint
  12.  
  13. # the following function creates a random 4-digit list which contains values
  14. # within the range of 0 to 9 for each element
  15. def randomFourDigitList():
  16. tmplist = []
  17. while len(tmplist) < 4:
  18. value = randint(0,9)
  19. if value not in tmplist:
  20. tmplist.append(value)
  21. return sorted(tmplist)
  22.  
  23. # converts the user input to a real list object with integer type elements
  24. def convertToList(inputString):
  25. list = [int(x) for x in inputString]
  26. return list
  27.  
  28. # actual game start, the player is being prompted to enter a four digit number
  29. print('Welcome to the Cows and Bulls Game!\nEnter a number:')
  30. bull = 0
  31. cow = 0
  32.  
  33. # a random list is being created, called tmplist, by the use of the previously
  34. # created method "randomFourDigitList"
  35. tmplist = randomFourDigitList()
  36.  
  37. # the user needs to enter a guess what the random list/digits look like
  38. userGuess = input()
  39.  
  40. # the script tests if the user really entered a four digit number.
  41. # if the input is sth. different the script prompts the user to enter a value again
  42. while not re.match('^\d\d\d\d$', userGuess):
  43. print ('Error! Make sure you only use four digits from 0 to 9 as your input.')
  44. userGuess = input('Try again: ')
  45. else:
  46. # that part is the actual comparison between the random list and the users input
  47. # it also prints out the cows and/or bulls which are hit
  48. userGuess = convertToList(userGuess)
  49. while tmplist != userGuess:
  50. for i, x in enumerate(tmplist):
  51. print(str(tmplist[i])+str(userGuess[i]))
  52. if int(tmplist[i]) == int(userGuess[i]):
  53. cow += 1
  54. elif int(tmplist[i]) != int(userGuess[i]):
  55. if int(userGuess[i]) in tmplist:
  56. bull += 1
  57.  
  58. cowString = ''
  59. bullString = ''
  60. if cow > 1:
  61. cowString = 's'
  62. elif bull > 1:
  63. bullString = 's'
  64.  
  65. print(str(cow)+' cow'+cowString+', '+str(bull)+' bull'+bullString )
  66. bull = 0
  67. cow = 0
  68. userGuess = input('Try again: ')
  69. userGuess = convertToList(userGuess)
  70. # once the user guesses all the digits correctly the game ends and prints out the
  71. # random number which has been guessed
  72. print("Success! The correct numbers are "+str(tmplist)[1:-1]+"!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement