Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions