[Tutor] Hangman game.....problem putting strings in a list.....
alan.gauld at btinternet.com
Wed Dec 22 02:37:11 CET 2010
Forwarding to tutor list, please use Reply All whjen replying to the group.
Author of the Learn To Program website
----- Original Message ----
> full error printout. I tried debugging it and I still don't know why
> guessed[index] = (letter) gives me an error. I don't want to use the append
> method because it will put the new letter at the end of the list which is
> very different then playing the hangman game..
OK, On reading it again I see why you are using the index approach.
> guessed = guessed * (len(line)-1)
That should be OK although personally I'd use a list comprehension
to initialise the list:
guessed = [None for c in line] # use the characters in line to get
> Traceback (most recent call last):
> File "/Python_Hello_World/src/HangMan/__init__.py", line 72, in <module>
> File "/Python_Hello_World/src/HangMan/__init__.py", line 56, in doit
> File "/Python_Hello_World/src/HangMan/__init__.py", line 15, in guess
> guessed[index] = (letter);
> TypeError: 'str' object does not support item assignment
I don't understand how you are getting this either, I don't get that error
in similar code. Can you insert a print statement just before that line like:
print 'index: ',index, '\tletter: ',letter,\nguessed: ', guessed
So we can see the values just before the line is executed.
Also drop the semi colon and parens. They shouldn't do any harm
but it removes two more variables from the equation...
> I am switching from java so my python style might be a bit unorthodox. I do
> however access my global variables in different methods... Don't know why it
> is encouraged in java and discouraged in python ?
I've never seen global variables being encouraged in Java. I think you may
be thinking of class (or instance) variables which are also ok in Python
albeit more clearly identified by prefixing with self (ie 'this' in Java).
> >> guessed = ["-"];
> >> count = 0;
> >> wrong = 0;
> >> def guess(letter):
> >> global guessed
> >> if (letter in line):
> > You don't need the parens, they don't do any harm,
> > but they aren't needed.
> >> index = line.index(letter);
> >> print guessed;
> >> # This is the line that gives me the error don't know why?
> >> guessed[index] = " " + (letter); ,TypeError: 'str' object does not
> >> support item assignment
> >> guessed[index] = (letter);
> > Again, you don't need the parens...
> > And I suspect you really want to use append() here rather
> > than assigning to guessed[index].
> >> print ' '.join(guessed)
> >> else:
> >> global wrong;
> >> wrong += 1;
> >> def draw(number):...
> >> def doit():
> >> global count
> >> while(wrong != 7):
> >> a_letter = raw_input("Pick a letter --> ")
> >> print
> >> guess(a_letter);
> >> draw(wrong);
> >> print
> >> count += 1
> >> def initArray():
> >> global guessed
> >> print line
> >> guessed = guessed * (len(line)-1)
> >> print "this is new list " + guessed;
> > If you use the append() method you don't need this.
> >> while 1:
> >> line = file.readline();
> >> if (len(line) >= 5):
> >> initArray()
> >> doit();
> >> break
> >> if not line: break
> >> file.close()
> > HTH,
More information about the Tutor