My code is going on forever | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
29th May 2022, 5:26 PM
Nandan Joshi
11 Answers
+ 3
"for char in string" iterates on every char of the string. BTW, you don't even need lists for that. Try to make it simpler. It's a very useful learning.
29th May 2022, 8:13 PM
Emerson Prado
Emerson Prado - avatar
+ 5
1. Yes. Every challenge in your logic skills is an important step. Good code comes from mastering logic. 2. It's a part of learning. 3. Experience. As you get better in solve logic stuff, you can go into more complex challenges.
29th May 2022, 10:32 PM
Emerson Prado
Emerson Prado - avatar
+ 2
I ran it several times, both with letters very common and absent in the word list, and it never went on forever. Analyzing your code: the while condition is tries > 0, and the loop necessarily decrements tries in every iteration. So it's impossible to run forever. Could you pls review your difficulty, then update the question description accordingly?
29th May 2022, 6:29 PM
Emerson Prado
Emerson Prado - avatar
+ 1
Run it in Code Playground and see for yourself. If there are no difficulties, just fix the code. If there are, I'm willing to help.
29th May 2022, 7:14 PM
Emerson Prado
Emerson Prado - avatar
+ 1
One thing I can say is the code is way overcomplicated. This makes it quite hard to debug. Some hints: 1. Instead of initializing variables with strings, then converting to lists, what about just initializing as lists already? 2. It seems the max number of tries is fixed. Why not a for loop, instead of a while loop, with decrements all over? 3. The elif condition is exactly the opposite of the previous if condition. Why not just an else? Also, the guessed_words variable gets initialized on every iteration. Why?
29th May 2022, 8:00 PM
Emerson Prado
Emerson Prado - avatar
0
I have no difficulty or anything its just a code and error And how come it isnt running forever for u i am using pycharm So i put this code and then it outputs my guess in its index forever So if word = [c, a, t] It outputs [_, a, _] forever
29th May 2022, 6:54 PM
Nandan Joshi
0
Ok dude thanks i made a few changes and now its lil but better It works almost properly 2 problems 1. It doesnt print 2 repaeting letters Like in apple it will only print ap_le 2. It prints world already guessed all the time This is some small placement problem i think
29th May 2022, 7:47 PM
Nandan Joshi
0
Now on the questions: 1. The code only checks if a word contains a letter, not all occurrences of it. Maybe you should iterate on letter occurrences, then substitute each one. 2. The first if block appends guessed letter in guessed letters list. The second checks if guessed letter is in the list. So it will always be true.
29th May 2022, 8:04 PM
Emerson Prado
Emerson Prado - avatar
0
1. How do i do this one 2. I removed fail it seemed unecessary so this one is solved and its much much better now
29th May 2022, 8:08 PM
Nandan Joshi
0
Another way is to check if guessed letter is in the word, so you can set the score, then use a string method to do all substitutions at once. I don't remember the method from my head, but you can search it in str class docs.
29th May 2022, 8:22 PM
Emerson Prado
Emerson Prado - avatar
0
Ok so i make an empty string and then joint it useing .join for display word so now its not a list Then i did what u said and did for char in guess ( guess is a string as input() is used ) It not only looks better but easier as well I made a very crude version of this completely on my own It wasnt that impressive but it did what i wanted so i moved on Now i did this And here i had 2 major problems after the loop one which idk why i faced 1. Was the double letter and 2. Was it priniting fail To solve 2. I just got rid of it which made jt better And thanks to ur help i did 1. Now i just have 3 off topic questions ( sorry ) 1. I started python two weeks ago so is making hangman a big deal 2. Not only here but other problems are there too where i have a small error and i have the knowlwdge to solve it but sometimes it just doesnt come to mind like for me using a string comes quicket than empty string 3. How does this compare to making actual games ?
29th May 2022, 8:24 PM
Nandan Joshi