I asked the following question and received the answer posted below. When I use
ID: 3590191 • Letter: I
Question
I asked the following question and received the answer posted below. When I use this code, Spyder tells me that there is an Unbound Local Error and that " local variable 'attempt' referenced before assignment." How do I fix this?? What do I assign the variable attempt?
Question:
Write an application in Python that allows a user to play the game Bulls and Cows against a computer. The game works as follows: The computer chooses a 4-digit number in secret. The digits must all be different. The user then guesses the number and the computer will provide the number of matching digits. If the matching digit is in the right position it is a "bull", if it is in a different position it is a "cow".
If the user guesses a number with repeat digits that is partially correct the rule is that a correct digit can only count once and bulls count before cows.
Your program should report the number of attempts the user needed to guess the number and it should let a user play as many times as they wish in a single session. Hint: you may find it easier to store these numbers as strings or in lists rather than as plain integers.
You must use the attached templates. Write the code that is needed to complete the three functions in the bulls_and_cows module and the play_game function in the game module. The play_game function should make use of the functions you write in the bulls_and_cows module. Do not modify the main function in the game module. Your code must work with the main function that is provided as is.
Template for cows_and_bulls.py:
import random
def generate_secret():
''' Generates a 4 digit number with no repeat digits''
It converts the number to a string and returns it'''
#add your code here
return secret
def how_many_bulls(answer,guess):
''' Returns the number of bulls the guess earns when the
secret number is answer. Both answer and guess should be strings'''
#add your code here
return bulls
def how_many_cows(answer, guess):
''' Returns the number of bulls the guess earns when the
secret number is answer. Both answer and guess should be strings'''
#add your code here
return cows
Template for game.py:
import bulls_and_cows as bc
def main():
# Do not change this function!
print('Welcome to Bulls and Cows death match!')
again='y'
while (again=='y'):
play_game()
again=input('would you like to play again? (y/n)')
print('So long sucker!')
def play_game():
''' Plays one interactive game of bulls and cows on the console'''
#add your code here
#call the main function to run the game
main()
Answer Given:
bulls_and_cows.py:
import random
def generate_secret():
###Generates a 4 digit number with no repeat digits
###It converts the number to a string and returns it
digits = list(range(10))
random.shuffle(digits)
return ''.join(map(str, digits[-4:]))
def how_many_bulls(answer,guess):
#Returns the number of bulls the guess earns when the
#secret number is answer. Both answer and guess should be strings
#add your code here
bulls = 0
for i in range(4):
if answer[i]==guess[i]:
bulls += 1
return bulls
def how_many_cows(answer, guess):
#Returns the number of bulls the guess earns when the
#secret number is answer. Both answer and guess should be strings
#add your code here
cows = 0
for i in range(4):
if answer[i]!=guess[i] and answer[i] in guess:
cows += 1
return cows
game.py:
import bulls_and_cows as bc
def main():
# Do not change this function!
print('Welcome to Bulls and Cows death match!')
again='y'
while (again=='y'):
play_game()
again=input('would you like to play again? (y/n)')
print('So long sucker!')
def play_game():
answer = bc.generate_secret()
attempt = 0
while True:
guess = input('Enter guess string:')
if guess.isdigit() and len(guess)==4:
attempt += 1
bulls = bc.how_many_bulls(answer, guess)
cows = bc.how_many_cows(answer, guess)
if bulls == 4:
print('You guessed it correct in %d attempts' % attempt)
break
else:
print('%d bulls ,%d cows' % (bulls, cows))
else:
print('Invalid guess')
#call the main function to run the game
main()
Explanation / Answer
import random
x = list(str(random.sample(range(1001, 10000), 1)))
x.remove("[")
x.remove("]")
print(x)
count = 0
cows = 0
def cows_and_bulls():
z = list(input("Please enter 4 digits:"))
if str("".join(z)) == "exit":
print("Thanks for the game!")
else:
global cows
cows = 0
bulls = 0
for i in range(4):
for j in range(4):
if z[i] == x[j]:
if i == j:
cows += 1
elif z[j] == x[j]:
bulls == bulls
else:
bulls += 1
print("Cows: " + str(cows))
print("Bulls: " + str(bulls))
while cows < 4:
cows_and_bulls()
count += 1
if cows == 4:
print("Congrats! You guessed " + str(count) + " times.")
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.