Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #Python exercise 18 - Cow and Bull
  2. import random
  3.  
  4. def score(num,rnd):
  5. cowsbull = [0,0]
  6. for i in range(len(rnd)):
  7. if num[i] == rnd[i]:
  8. cowsbull[0]+=1
  9. elif num[i] in rnd:
  10. cowsbull[1]+=1
  11. return print("Cows",cowsbull[0],"Bulls",cowsbull[1])
  12.  
  13.  
  14. if __name__=="__main__":
  15.  
  16. tryagain = 'Y'
  17. cnt = 0
  18. rd = str("%0.4d" % random.randint(0,9999))
  19.  
  20. print("=================================")
  21. print("Welcome to the Cow and Bull game!")
  22. print("=================================""\n")
  23. print("To win, guess the four digit number""\n")
  24. print("Cows mean you have a correct digit in the right place""\n")
  25. print("Bulls mean the digit is right, but the wrong place""\n")
  26.  
  27. while tryagain in ['Y','y']:
  28. gs = input("Please enter your guess or 'Q' to quit: ")
  29. cnt += 1
  30. while len(gs) == 4 or gs not in ['Q','q']:
  31. if gs == rd:
  32. print("Congratulations, you've guessed the number in",cnt,"tries!")
  33. choice = input("Try again? Y/Any other key to quit: ")
  34. if choice in ['Y','y']:
  35. tryagain = 'y'
  36. else:
  37. break
  38. elif gs in ['Q','q']:
  39. break
  40. else:
  41. score(gs,rd)
  42. break
  43. else:
  44. if gs in ['Q','q']:
  45. break
  46. else:
  47. print("Invalid input - try again")
  48. print("Thanks for playing, come back soon")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement