So I am trying to add a graphical element to my game. And it draws a circle for white or black pegs. However, the list that contains "Black" and "White" isn't acting quite like I want.

The list prints like this:
B
l
a
c
k

B
l
a
c
k

But I want it to print like:
Black Black

so I can do this:

print len(self.hints)
     for i in range(len(self.hints)):
          print self.hints[i]
	  if self.hints[i] == "Black":
	       blackPeg = Circle(Point(50 + i*30, 250), 12)
	       blackPeg.setFill("black")
	       blackPeg.setOutline("black")
	       blackPeg.draw(win)
	  elif self.hints[i] == "White":
	       whitePeg = Circle(Point(200 + i*30, 250), 12)
	       whitePeg.setFill("white")
	       whitePeg.setOutline("white")
	       whitePeg.draw(win)

This generates the Black and White list:

class Hints:
     def __init__(self, guess, secretCode):
          self.guess = guess
	  self.secretCode = secretCode

     def hints(self):	
#Creates a list for the hints
          hint = []
	  guessCopy = list(self.guess)
	  secretCodeCopy = list(self.secretCode)
	  for i in range(len(guessCopy)):
	       if guessCopy[i] == secretCodeCopy[i]:
		    hint.append("Black")
		    secretCodeCopy[i] = "X"
		    guessCopy[i] = "Y"
	  for i in range(len(guessCopy)):
	       for j in range(len(secretCodeCopy)):
		    if guessCopy[i] == secretCodeCopy[j]:
		         hint.append("White")
			 secretCodeCopy[j] = "Z"
			 break
		#If no numbers in the guess are correct, there are no hints given
	  if len(hint) == 0:
	       return ""
	  hint.sort()
          return " ".join(hint)

Thanks for the help.

return " ".join(hint) creates a string by joining strings in the list, separated by a space. For example, if you have the following list you strings:

["black", "white", "black"]

then you would end up returning "black white black" .

Now when you go to your loop, assuming that self.hints is what you returned from your hints method, then self.hints is a string. Therefore, for i in range(len(self.hints)): will loop through one character at a time instead of one hint value at a time ("black" or "white").

The remedy? Try returning hint untouched from your hints method.

Thanks a bunch. Worked perfectly.

In my main code, I have print str(hints.hint()), which gives:

Pegs:

Is there a way to make it print just:
Black Black Black Black Black Black

Yeah:

print " ".join(hints.hint())

Which should combine the list into a string, separated by a space. But you knew that before, didn't you?

Figured that out. That was a dumb question. :P

But, on a more intelligent note, is there a way to limit these questions to certain answers:

#Limit the answer to a combination of r g b y o p, if it contains something other than those six, to ask the question again (I know how to ask again, I just don't know how to limit to six certain characters)
colorCode = raw_input("Player 1 enter the secret code. No cheating player 2! ")

#Limit the answer to numerical characters only. If not numerical, ask again.
maxGuesses = input("How many guesses do you want to give player 2? ")

Thanks.

Use a list for the correct responses within some kind of while loop

exit = False
while not exit:
   #Limit the answer to a combination of r g b y o p
   colorCode = raw_input("Player 1 enter the secret code. No cheating player 2! ")
    if colorCode in ['r', 'g', 'b', 'y', 'o', 'p']:
       ## OK so ask next question
       #Limit the answer to numerical characters only. 
       # If not numerical, ask again.
       maxGuesses = raw_input("How many guesses do you want to give player 2? ")
       if maxGuesses.isdigit():
           H = Hints(int(maxGuesses), colorCode)   ## or whatever
           ##  exit loop
           exit = True

Thanks a bunch!

Thanks a bunch!

I gave it a try and modified what you did to fit what I am trying to do:

exit1 = False
while not exit1:
	colorCode = raw_input("Player 1 enter the secret code. No cheating player 2! ")
	if colorCode in ['r', 'g', 'b', 'y', 'o', 'p']:
		exit1 = True
exit2 = False
while not exit2:
	maxGuesses = input("How many guesses do you want to give player 2? ")
	if maxGuesses.isdigit():
		exit2 = True

However, the colorCode part does not allow for things like rgb if they want the secret code to be more than one color.

The maxGuesses part crashes if you enter something like geh.

How many guesses do you want to give player 2? geh
Traceback (most recent call last):
File "mastermind.py", line 166, in <module>
main()
File "mastermind.py", line 69, in main
maxGuesses = input("How many guesses do you want to give player 2? ")
File "<string>", line 1, in <module>
NameError: name 'geh' is not defined

If you enter a number:

How many guesses do you want to give player 2? 10
Traceback (most recent call last):
File "mastermind.py", line 166, in <module>
main()
File "mastermind.py", line 71, in main
if maxGuesses.isdigit():
AttributeError: 'int' object has no attribute 'isdigit'

What did I screw up?

What did I screw up?

You used input and you're not using Python30. For anything prior to Python30 you should be using raw_input like you did for the earlier user input.

Try entering the following into input and see what happens: 4 * 10 + 2 / 5 .

You used input and you're not using Python30. For anything prior to Python30 you should be using raw_input like you did for the earlier user input.

Try entering the following into input and see what happens: 4 * 10 + 2 / 5 .

That was an obvious error I did not catch. I keep forgetting a lot of people are using Python 3.0 and there are differences. I have to use Python 2.5 because that is what my school uses and my professor uses.

So how do I get the colorCode part to accept a string of letters that are in that list of six vs. just one letter.

I want inputs like:
r
rg
rgb
rggg
rgbyop
pooybg
etc.
Length of the string is not important, but being able to enter multiple characters + multiples of each character is important.

Thanks a bunch.

So how do I get the colorCode part to accept a string of letters that are in that list of six vs. just one letter.

You can loop over the user's input like this:

colorCode = raw_input("Enter the secret code: ")
for each_color in colorCode:
    if each_color in ['r', 'g', 'b', 'y', 'o', 'p']:
        # Pass condition
    else:
        print "The color choice %s is not valid" % each_color

I notice that you're still using tabs. I suggest you save yourself future heartache and switch to 4 spaces instead. Most text editors can be setup to use 4 spaces instead of tabs.

You can loop over the user's input like this:

colorCode = raw_input("Enter the secret code: ")
for each_color in colorCode:
    if each_color in ['r', 'g', 'b', 'y', 'o', 'p']:
        # Pass condition
    else:
        print "The color choice %s is not valid" % each_color

I notice that you're still using tabs. I suggest you save yourself future heartache and switch to 4 spaces instead. Most text editors can be setup to use 4 spaces instead of tabs.

Thanks for the help. Worked wonderfully.

May I ask why four spaces are better than tabs? I was just taught this term in college to use tabs...

Edit: I just checked and the tab is set to 4 spaces. Weird.

May I ask why four spaces are better than tabs? I was just taught this term in college to use tabs...

Tabs are unruly. Some systems translate a tab character ( \t ) to represent 4 spaces, while others 8 spaces. This could potentially be set to be anything (how about 55 spaces? Sure!) by the user; however on most standard installations it's either 4 or 8.

So let's say you're coding and using tabs and then some spaces on a line or two. You finish your epic text adventure and then send the script off to your friend. Well suddenly your script is completely broken because their system mis-aligns your indentations due to a different tab setting.

Also (and probably most importantly) Mr. Python himself, a.k.a. Guido van Rossum said he regrets ever allowing Python to accept tabs in the first place. He suggests that everyone use 4 spaces. Whoever is teaching you to use tabs needs to read PEP 8, and it wouldn't hurt for you to do the same.

I don't really agree that spaces are the proper way and tabs aren't. I would agree that which ever you choose, you should use it consistently. There are some cases where tabs can be convenient (being stuck with a retarded editor comes to mind) and won't matter if you use them consistently for indentation throughout. If code that you produce is known to use tabs throughout for indentation, it wouldn't be too much trouble for someone wanting to use spaces instead to just convert them to the appropriate amount.

That said if you're going to go with spaces, then you're better off just using 4, since most python code out in the wild would use 4 spaces for indentation anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.