Created
July 17, 2017 11:09
-
-
Save JagDecoded/6e973effa2397ffd97b1cb5b9d8a146a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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