Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JagDecoded/6e973effa2397ffd97b1cb5b9d8a146a to your computer and use it in GitHub Desktop.
Save JagDecoded/6e973effa2397ffd97b1cb5b9d8a146a to your computer and use it in GitHub Desktop.
# Code written in PYTHON 3.6
# Cow & Bulls Game - a game where a 4 digit number will be generated automatically and user have to guess that number.
# For every perfectly guessed digit at perfect place user get a cow
# For every perfectly guessed digit at wrong place user get a Bull {Bull = Total matching digit - cows}
# game continues untill player guess the number exactly {4 cows} At the end the code will show how many guess count.
# Any improvement suggestion are welcome. Am a learner ready to Learn new things.
import random
def cowBull_loop():
ask= str(input("enter a four digit number: "))
cow=0
bullCow=0 #total number of digit guessed perfectly. right place + wrong place
for i in range(0,4):
if num[i]==ask[i]:
cow+=1
for i in num:
if i in ask:
bullCow +=1
bull=bullCow-cow
print("you have {} cow and {} bulls".format(cow,bull))
return cow
if __name__=='__main__':
num= str(random.randrange(1000,9999)) # for testing use str(2727)
cow= 0
count=0
while cow !=4:
count+=1
cow = cowBull_loop()
@matheuspena8
Copy link

The review still has a problem. When the num created has a repeted number (lets say 11ab) and someone guesses only one number correctly of the num (eg: 1cde), the game will return 1 cow and 1 bull or 2 bulls instead of only 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment