Create a bulls and cows game for guessing a randomly generated 4 digits number.
This one should be easier for you. Just make sure that the random number is four digits. Hence, it should be between 1000 and 9999.
To calculate the bulls and cows,
importrandomsecret_number=str(random.randint(1000, 9999)) print("Guess the number. It contains 4 digits.") remaining_try=7defget_bulls_cows(number, user_guess): bulls_cows= [0,0] #cows, then bullsforiinrange(len(number)): ifuser_guess[i] ==number[i]: bulls_cows[0] +=1elifuser_guess[i] innumber: bulls_cows[1] +=1returnbulls_cowswhileremaining_try>0: player_guess=input("Enter your guess: ") ifplayer_guess==secret_number: print("Yay, you guessed it!") print("YOU WIN.") breakelse: bulls_cows=get_bulls_cows(secret_number,player_guess) print("Bulls: ",bulls_cows[0]) print("Cows: ",bulls_cows[1]) remaining_try-=1ifremaining_try<1: print("You lost the game.") break
After solving the Bulls and cows for 2 digits, this one should become easier for you.
However, solving this one by using if-else that we did for 2 digit bulls and cows game will become very harder.
That's why we created the get_bulls_cows function. Inside that, we created a loop. For each digit, check the digit in the same place.
tags: programming-hero
python
python3
problem-solving
programming
coding-challenge
interview
learn-python
python-tutorial
programming-exercises
programming-challenges
programming-fundamentals
programming-contest
python-coding-challenges
python-problem-solving