Skip to content

Instantly share code, notes, and snippets.

@bgschiller
Last active November 22, 2016 01:11
Show Gist options
  • Save bgschiller/87285c25f8e8a3f4c6956db78cacdb47 to your computer and use it in GitHub Desktop.
Save bgschiller/87285c25f8e8a3f4c6956db78cacdb47 to your computer and use it in GitHub Desktop.
Cows and Bulls exercise

Cows and Bulls

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 the game and tell the user at the end.

Say the number generated by the computer is 1038. An example interaction could look like this:

Welcome to the Cows and Bulls Game! 
Enter a number: 
>>> 1234
2 cows, 0 bulls
>>> 1256
1 cow, 0 bulls
...

Until the user guesses the number.

Some additional challenges:

  1. Print out an error if the user enters something other than a 4-digit number. Don't count this invalid guess against their total.
  2. Correctly pluralize your output (0 cows, 1 cow, 2 cows, etc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment