Skip to content

Instantly share code, notes, and snippets.

@odanga94
Created May 16, 2017 05:56
Show Gist options
  • Save odanga94/f5daca226a8c69eb50f9968870b4b010 to your computer and use it in GitHub Desktop.
Save odanga94/f5daca226a8c69eb50f9968870b4b010 to your computer and use it in GitHub Desktop.
PracticePython/Exercise18.py
import random
def play():
true_value = str(random.randint(1000, 9999))
guesses = 0
play = 'y'
while play == 'y':
cows = 0
bulls = 0
guess = int(input("Enter a 4-digit number: "))
if guess not in range(1000, 9999):
print("Please enter a 4-digit number.")
continue
guess = str(guess)
guesses += 1
if guess == true_value:
print("4 cows! You won")
break
else:
for index, item in enumerate(guess):
if guess[index] == true_value[index]:
cows += 1
elif item in true_value and guess[index] != true_value[index]:
bulls += 1
print("%d cows, %d bulls") %(cows, bulls)
play = raw_input("play game ?('y' to play)")
print("You made %d guess(es)") %(guesses)
print("The correct number was %s") %(true_value)
if __name__ == "__main__":
play()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment