[Tutor] Python Mastermind Help
Alan Gauld
alan.gauld at btinternet.com
Thu Jan 30 13:07:56 CET 2014
On 29/01/14 22:19, Amy Davidson wrote:
> I’ve been given an assignment in which I need to recreate
> the game Mastermind.
OK I assume you know how mastermind works?
ie the rules of the game.
> I’ve been attempting it for approx a week with little to no progress.
> I was hoping someone would be able to point me in the right direction.
OK, Start by writing down an example of what the finished
game should look like. Maybe a welcome message followed by
a prompt for input. An example input and the games response
and so on until it completes.
That will act as a specification for your program.
> here is what I’ve got:
>
> import random
I assume you plan on using random to generate the target pattern?
For now I'd start with a fixed pattern, generating a random one is best
left to the end otherwise testing behaviour will be slightly more difficult.
> def masterMind():
> userGuess = raw_input("Guess my 5 digit password:”)
>
> while True:
> if len(userGuess) != 5:
> userGuess = input("Guess my 5 digit password:”)
When you create a "while True" loop its vital that you provide some way
to exit. I usually code the exit clause immediately after creating
the loop.
Your loop has no exit so it just loops forever.
If the len() is not 5 you get a prompt and another attempt
but if it does equal 5 the loop just spins silently.
Your next steps should be:
1) Clarify in your mind what the program should do (see above)
2) create a means of exiting the loop
3) create a target to guess
4) start comparing the user input to the target. ( I suggest
you create a second function to do that, you can then test
it in isolation)
Let us know how you get on.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list