No BS: Make sure you read the client specification properly and clearly. Otherwise you will end up attempting to deliver something that they didn’t even ask for, and ultimately, wasting your time.
With BS: So while attempting Exercise 18from https://www.practicepython.org, I ran into a bit of a dilemma. What I should have done was read the exercise clearly and ensure that my program meets the requirements for what is being asked.
What I did instead was complete what was being asked, check a few of example that might throw errors. For example:
Suppose the answer in the game is 1109. You then guess 1111. The result that the program spits out would be “2 Cows, 2 Bulls”. 2 Cows is correct, we do indeed have a 1 in the first and second positions. The 2 Bulls however I had thought was incorrect, while we have a 1 in the third and fourth position of our guess (and there is indeed a 1 elsewhere in the answer) I thought these should have been ignored.
I had been attempting to code a numbers version of the board game “Mastermind”. Since the rules of Cows & Bulls, and Mastermind are very similar I had jumped to an incorrect conclusion.
Ultimately, I ended up wasting a load of my own time. Well, kind of, I did learn the hard way that you should really, really, really read what the client wants.
And also, just a personal thought, this Cows & Bulls game is really just an unrefined version of the “Mastermind” game, to be honest.
For those interested in my code, it is as follows:
### Cows and Bulls game. Generate 4-digit random number. Have the user make a guess ### and for every digit they guess correctly (but in the wrong position) then they ### get a bull. For every digit guessed correctly and in the right position, they get ### a cow. Once fully guessed, the game is over. # import 'random' module for use import random #Takes input of an 'answer' and an integer to compare to the 'answer'. def cowbull(ans,userin): # Set bull and cow counter to 0 bull = 0; cow = 0; # Check guess against answer and give feedback based on position for i in range(0,4): #range(0,4) = 0,1,2,3 if userin[i] == ans[i]: cow = cow + 1 elif userin[i] in ans: bull = bull + 1 return cow, bull # MAIN METHOD # if __name__ == "__main__": # Generate our 4 digit random number ans = str(random.randint(0,9999)) while len(ans) < 4: ans = '0' + ans g = 0 # Set guess counter to 0 playing = True # Set playing state # Begin while loop to check guess until true, also include a line to quit while playing == True: # Ask user for a guess userin = input("Guess what 4-digit number I'm thinking of, or type 'quit' to exit: ") # Increase guess counter g = g + 1 # Quit condition if userin == 'quit': break if userin == 'hint': print(ans) # Win condition if userin == ans: print('Congratulations, you won in',g,'guesses') break cow, bull = cowbull(ans,userin) #Show clues to user print('Cows',cow,', Bulls',bull)