Alright.. Seems I wasn't able to fully explain my problem.. Below is the 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..
Word = 'python'
- - - - - -
Pick a letter --> h
If I use append it will put it at the end instead..
- - - - - - h
I was wondering if this line contributes to the error. Because before this I had the size of the guessed hardcoded and it worked perfectly.
guessed = guessed * (len(line)-1)
Traceback (most recent call last):
File "/Python_Hello_World/src/HangMan/__init__.py", line 72, in <module> doit();
File "/Python_Hello_World/src/HangMan/__init__.py", line 56, in doit guess(a_letter);
File "/Python_Hello_World/src/HangMan/__init__.py", line 15, in guess
guessed[index] = (letter);
TypeError: 'str' object does not support item assignment
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 ?
On 12/21/10 3:21 AM, "Alan Gauld" <firstname.lastname@example.org> wrote:
> "Yasin Yaqoobi" <email@example.com> wrote
> I'm confused. The error message you describe doesn't
> appear to match any line in your code.
> Please provide the full error printout not just a single line.
> Meanwhile some comments...
>> global line
>> global index;
> global is not doing anything here, it is only effective inside a
> Try not to use global variables unless you have to. Specifically only
> for data that's shared between functions, and even then it's usually
> better practice to pass the values into the functions as arghuments.
>> 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)
>> global wrong;
>> wrong += 1;
>> def draw(number):...
>> def doit():
>> global count
>> while(wrong != 7):
>> a_letter = raw_input("Pick a letter --> ")
>> 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):
>> if not line: break