Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Python exercise 18 - Cow and Bull
- import random
- def score(num,rnd):
- cowsbull = [0,0]
- for i in range(len(rnd)):
- if num[i] == rnd[i]:
- cowsbull[0]+=1
- elif num[i] in rnd:
- cowsbull[1]+=1
- return print("Cows",cowsbull[0],"Bulls",cowsbull[1])
- if __name__=="__main__":
- tryagain = 'Y'
- cnt = 0
- rd = str("%0.4d" % random.randint(0,9999))
- print("=================================")
- print("Welcome to the Cow and Bull game!")
- print("=================================""\n")
- print("To win, guess the four digit number""\n")
- print("Cows mean you have a correct digit in the right place""\n")
- print("Bulls mean the digit is right, but the wrong place""\n")
- while tryagain in ['Y','y']:
- gs = input("Please enter your guess or 'Q' to quit: ")
- cnt += 1
- while len(gs) == 4 or gs not in ['Q','q']:
- if gs == rd:
- print("Congratulations, you've guessed the number in",cnt,"tries!")
- choice = input("Try again? Y/Any other key to quit: ")
- if choice in ['Y','y']:
- tryagain = 'y'
- else:
- break
- elif gs in ['Q','q']:
- break
- else:
- score(gs,rd)
- break
- else:
- if gs in ['Q','q']:
- break
- else:
- print("Invalid input - try again")
- print("Thanks for playing, come back soon")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement