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[0] * (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 ?

Sincerely,
Yasin Yaqoobi


On 12/21/10 3:21 AM, "Alan Gauld" <alan.gauld@btinternet.com> wrote:

>
> "Yasin Yaqoobi" <yasinyaqoobi@gmail.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
> function.
>
> 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)
>> 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[0] * (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()

Recommended Answers

All 3 Replies

String is not mutable, transform string to list by doing word = list(word)

Yes, this is the problem: TypeError: 'str' object does not support item assignment

Make your word a list of single character strings as tonyjv suggests, and you shouldn't have to change any of your other code.

Thank you very much guys... That solved my problem...

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.