hey guys, i wrote this, but there is a mistake, when you guess a right letter. it says: 'IndexError: list assignment out of range' word = ['c','o','c','o','n','u','t'] length = len(word) hidden = ['_ '*length] number = int(0) mistakes = ['-'] print (hidden) while number<=10: letter = str(input('Enter a letter: ')) if letter in word: i = 0 while i < length: # indexError: list assignment index out of range if letter is word[i]: hidden[i] = letter i = i+1 else: i = i+1 print(hidden) else: number = number+1 print ('This letter is not in here...') print (mistakes*number) print (hidden) print ('You failed!') can you help me?
7/8/2017 11:20:35 AMAnika Schmalfeld
3 AnswersNew Answer
To fix your error, you need to write: hidden = ['_']*length ... as else ['_'*length] will procude a list with an unique item wich is a string of 'length' number of '_' char, instead of a list of 'length' items '_' ;) Anyway, your code not really still work, as your second mistake is in your conditional statement: if letter is word[i]: ... wich doesn't work as you expect: the result is True only if the two referenced variable are the same (not only if values stored are equals) ^^ To fix this, replace 'is' keyword with equality operator '==': if letter == word[i]: However, you should also correct your code, as it's not detecting when all letters of searched word are found, and your 'while' loop seems to run infinitely (there should be a problem with the 'number' counter, wich is only increased on letter fail -- so the script will run while user don't have make 10 errors :P)
thanks a lot for your help! :) It works now, except of the last point, but i'll get it, i think... :)
Here is my code if hangman game, may be interesting https://code.sololearn.com/cu5xv3hUCF90/?ref=app