I've been writing a program (a hangman game) today, and have come across a few snags, I know you guys don't like people just using this as answers to their homework, but I have searched quite a while, and while I have posted this program earlier in the development, I have come to new ground. I am getting to grips with lists, tuples, dictionaries, etc. and this program has been great practice. Sorry for the wall of text!
The Question:
Is there a way to refer to a string when using a list?
Context - defining a function (last line being a test to see if working)
def click_1 (text):
key_1 = word.index [letter]
hidden_word[key_1] = letter
print (hidden_word)
This throws up a long error, which I wont post, here is the 'important' part in my eyes:
File "/Users/foo/Desktop/Hangman.py", line 19, in click_1
key_1 = word.index [letter]
TypeError: 'builtin_function_or_method' object is not subscriptable
If someone could help me, that would be great. I've tried using the 'in' statement, but I have not been able to make it work - currently, though I suspect it may be part of the solution. I've tried:
key_1 = word.index [letter in word]
and:
key_1 = word.index([letter] in word)
both of which do not work.
Cheers in advance from a young programmer!
wordis not what you suspect it is. What is the output oftype(word)just before the error?key_1 = word.index(letter)textbut not using it in the function. How are the other variables you do reference generated and whattypeare they?wordis; 2. what typeletteris; 3. exactly what you want to do with this line of code that you can't get working. It would help to show specific examples of a value forword, a value forletter, and exactly what you wantkey_1to end up being in each case, showing your reasoning. The reason I say this is because as it stands, I have absolutely no idea what you're trying to ask. What do you mean by "refer to a string when using a list"? What do you mean by "using a list"? Refer to what string?