Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Python programming to play \'bulls and cows\' game/ computer guesses the number

ID: 3728447 • Letter: #

Question

(Python programming to play 'bulls and cows' game/ computer guesses the number)

Hello every one

Here is my question!

I should write a program to play Bulls and Cows!!

Host(human player) should think about the secret number and the program should guess. Host should give hints to the program.

1. You are free to implement your program for 3-digit in python.


2. In the beginning of each game, indicate to the host(human player) the number of digits your program is designed to guess.


3. Inform the host to choose a “secret number” with no repeated digits and have it written down somewhere.


4. Prompt “host” to start the game(the program)


5. Your program as “game-player” starts to guess, after each guess, prompt “host” for feedback as described in the “Overview” session:
a. How many correct digits?[ex) if the secret number is 1234 and the guessed number is 4719, 2 corrects]
b. How many exact digits? (skip this question if Correct is 0)[(ex0)if the seceret number is 1234 and the guesed number is 1538, 2 exact]


6. Your program is required to verify the responses returned by the “host”. If needed, repeatedly prompt “host” to re-enter the correct information, such examples as:
a. Non-numeric responses
b. Value of “Exact” is greater than that of “Correct”
c. Value of either “Correct” or “Exact” should be less than the length of the secret number (3 or 4)
d. Inconsistent responses


7. Feedback History
a. Keep a list of history of all guesses including corresponding feedback, and then displaythe entire list after each guess in chronological order


8. Termination
a. the current guess matches the secret number
i. In this case, display a congratulation message
b. the number of guess attempts reaches the limit
i. Display a game-over message
c. the host enters “x” on any of the 2 feedback (Correct or Exact)


9. Termination Prompt
a. After termination, prompt “host” to quit or to continue a new game.


10. Game Startup
a. Show a welcome message to briefly describe what your program does
b. Then prompt “host” for the secret number prior to starting the game


11. For 3-digit number implementation, your program MUST successfully guessed the secret number within '20' attempts.

Explanation / Answer

import random

def compare_numbers(number, user_guess):
cowbull = [0,0] #cows, then bulls
for i in range(len(number)):
if number[i] == user_guess[i]:
cowbull[1]+=1
else:
cowbull[0]+=1
return cowbull

if __name__=="__main__":
playing = True #gotta play the game
number = str(random.randint(0,9999)) #random 4 digit number
guesses = 0

print("Let's play a game of Cowbull!") #explanation
print("I will generate a number, and you have to guess the numbers one digit at a time.")
print("For every number in the wrong place, you get a cow. For every one in the right place, you get a bull.")
print("The game ends when you get 4 bulls!")
print("Type exit at any prompt to exit.")

while playing:
user_guess = input("Give me your best guess!")
if user_guess == "exit":
break
cowbullcount = compare_numbers(number,user_guess)
guesses+=1

print("You have "+ str(cowbullcount[0]) + " cows, and " + str(cowbullcount[1]) + " bulls.")

if cowbullcount[1]==4:
playing = False
print("You win the game after " + str(guesses) + "! The number was "+str(number))
break #redundant exit
else:
print("Your guess isn't quite right, try again.")