+ 1

Why isn't this Python code working?

I'm trying to create a simple program in speed to help me with one task in the "Keep talking and nobody explodes" game where you have a bunch of words, and the other person flips through available letter in order for you to determine what the correct word from the bunch is. But no matter what i input, I always get this and I don't know why: File "D:/Code/words.py", line 10, in check list.remove(word) ValueError: list.remove(x): x not in list Code is here: list = ["about","after","again","below","could","every","first","found","great","house","large","learn","never","other","place","plant","point","right","small","sound","spell","still","study","their","there","these","thing","think","three","water","where","which","world","would","write"] def check(p): while len(list) != 0: letter = input("Letter " + str(p+1) + ": ") for word in list: while p < len(word): if word[p] != letter: list.remove(word) p += p print(list) check(p) check(0)

18th Mar 2017, 10:43 PM
David
David - avatar
4 Answers
+ 6
Ok. Let me follow the logic: After defining the function 'check' you run it with zero as argument. This means that inside the function the parameter 'p' = 0. Inside the function you have 3 loops nested one inside the other. The outer loop stops only when the 'list' is empty. (note: you should not use a Python keyword as a variable name.) The program asks for a letter which I will imagine, the user enters 'z'. After that, we enter another loop. A for loop. which will run for each word of 'list'. In the first iteration of the for loop 'word' = "about". We enter yet another loop. The inner loop. This loop will run while 'p' is smaller than length of "about". In other words. while 0 < 5: NOTE: The only way we could leave this inner while loop is if 'p' was incremented. But the statement that was supposed to increment it says: 'p += p' Since, 'p'= 0, you are incrementing 'p' to zero. Meanning, you are not incrementing it. We have an infinite loop here. CONTINUING: Inside the inner loop there's this if: if word[p] != letter: list.remove(word) In other words: if the first letter of the word "about" is not the letter entered by the user, remove it from 'list'. If the user entered 'z', the condition will be true ('a' != 'z'). This means "about" is removed from 'list'. BUT THE LOOP IS INFINITE, zero is still less than 5. This means, the statement: list.remove("about") will run again but "about" is already removed. that's why you get the ValueError. If the user insert the letter 'a' you would still be stuck in an infinite loop.
19th Mar 2017, 12:22 AM
Ulisses Cruz
Ulisses Cruz - avatar
+ 1
there is a few things wrong, but to answer your question, you have for word in list: while p < len(word): if word[p] != letter: list.remove(word) then you are trying to remove a word. your using word as a variable.. to keep it clear, use i for i in list: then you run a loop while p is smaller than word, which is 1. because your using word as a variable for each individual item in your list.
18th Mar 2017, 11:44 PM
LordHill
LordHill - avatar
+ 1
your code has a bunch of problems. it shows you are somewhat new. it also shows that you are trying. that means you will succeed. don't let this discourage you. take things 1 problem at a time work thru them. it's a great feeling when you run your program and it does what it's suppose to do. you can do this. Sometimes it's best to step back. don't be afraid to erase chunks of code that aren't working and approach your problem from a different angle.
19th Mar 2017, 12:55 AM
LordHill
LordHill - avatar
+ 1
I rewrote the code and got it to work. Thank you for your help and motivation !
20th Mar 2017, 5:19 PM
David
David - avatar