Created
September 8, 2016 04:48
-
-
Save jampola/cfd200542a08e4ac5685598587b217eb to your computer and use it in GitHub Desktop.
My contribution for http://www.practicepython.org/exercise/2014/07/05/18-cows-and-bulls.html
This file contains 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
from random import randint, randrange | |
number = [randrange(1,9) for x in range(4)] | |
guesscount = 1 | |
def game(guess): | |
cows = 0 | |
bulls = 0 | |
global guesscount | |
if guess != ''.join(str(x) for x in number): | |
guesscount+=1 | |
for x in range(len(number)): | |
if int(guess[x]) in number: | |
if int(guess[x]) == number[x]: | |
cows+=1 | |
else: | |
bulls+=1 | |
print("{} cows, {} bull").format(cows,bulls) | |
else: | |
print("You got it! It took you %s guesses") % guesscount | |
exit() | |
def main(): | |
while True: | |
myguess = raw_input("Guess: ") | |
game(myguess) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment