Skip to content

Instantly share code, notes, and snippets.

@arjun921
Created December 26, 2016 17:07
Show Gist options
  • Save arjun921/de518d52de8f9f265c4850021a94e536 to your computer and use it in GitHub Desktop.
Save arjun921/de518d52de8f9f265c4850021a94e536 to your computer and use it in GitHub Desktop.
Create a program that will play the “cows and bulls” game with the user. The game works like this: Randomly generate a 4-digit number. Ask the user to guess a 4-digit number. For every digit that the user guessed correctly in the correct place, they have a “cow”. For every digit the user guessed correctly in the wrong place is a “bull.” Every ti…
import random
def game(cow,bull,rnum,unum):
rnum = random.randint(1000,9999)
#print (rnum)
unum = int(input('Enter your guess:'))
for i,j in zip(str(rnum),str(unum)):
if i==j:
cow = cow+1
else:
bull = bull+1
print('Cows: {}'.format(cow))
print('Bulls: {}'.format(bull))
return cow,bull,rnum,unum
rnum,unum = 0,0
bull,cow = 0,0
condition = True
while(condition):
cow,bull,rnum,unum = game(cow,bull,rnum,unum)
if rnum==unum:
condition = False
@arjun921
Copy link
Author

Question:
Create a program that will play the “cows and bulls” game with the user. The game works like this:

Randomly generate a 4-digit number. Ask the user to guess a 4-digit number. For every digit that the user guessed correctly in the correct place, they have a “cow”. For every digit the user guessed correctly in the wrong place is a “bull.” Every time the user makes a guess, tell them how many “cows” and “bulls” they have. Once the user guesses the correct number, the game is over. Keep track of the number of guesses the user makes throughout teh game and tell the user at the end.

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