Please help I'm new my code has a bug and I dont' understand it

counts = 0
import random
x = random.randint(0,5)
wordlist = ["banana" , "apple" , "kiwi" , "blueberry", "mango" , "cherry" ]
hangmanword = wordlist[x]
print("_" * len(hangmanword))
guess = input(">").lower()
while counts < len(hangmanword) + 5:
    if guess in hangmanword:
        print("you got 1 letter of the hangman word right!")
        guess2 = input(">")
        if guess2 in hangmanword.replace(guess, " "):
            print("You got the second guess right too!")
        else :
             print("test run1")
    else:
        print("I'm sorry that's not in the word")
    counts += 1
else :
    print("Sorry, you don't have anymore chances left. Play again")

Guys, this is how you post code right, you use 3 backticks right?
this code shows the error
File “/Users/mylaptop/Desktop/python folder/hangman.py”, line 15
else:
IndentationError: expected an indented block after ‘else’ statement on line 14

I’m trying to make the Hangman game.
Thank you for your time in reading this post!
I haven’t added anything in the else because

Yes you quoted that correctly (probably). Thanks, that’s essential.

Now the indenting here is slightly off (one more space than is conventional) but that wouldn’t be an error. However, I wonder if you have a mixture of hard tabs and spaces (or some other non-printing character) that not even your excellent forum quoting can show us. On that theory:

  1. If your IDE allows it, turn on display of “whitespace” characters. (Now you might be able to see what is wrong.)
  2. Set your IDE so TAB indents by 4 spaces.
  3. Make sure print("test run1") (and everything else) is indented using only spaces.

In Python (as you might know) indentation is a part of the grammar, so it is important to indent your code properly.

Of course there is a question what is a “proper” value for the Python interpreter.
You see from the Python interpreter point of view tabs and spaces has the same meaning, so the way the Python interpreter decides if how much a line is indented is to just count any whitespace and the compare that with the previous indentation. If they are the same it means we are in the same block.

So, it all means when you choose to indent something, you have to be consistent.
If for example you decide to use spaces, tabs or any combination of these you have to do it in the same way in all your python code.

hey just a question, why is it essential to qoute the code properly, either way anyone still needs to copy paste the code in an IDE or code editor?

Thank you for your time and help

Thank you for your time and help, another question → i have to wait and depend on someone else answering and figuring out why my code doesn’t work, what do i do to be more productive instead of waiting on someone else to figure out why my code doesn’t work?

Because if you don’t it looks like this:

while counts < len(hangmanword) + 5:
if guess in hangmanword:
print(“you got 1 letter of the hangman word right!”)
guess2 = input(“>”)
if guess2 in hangmanword.replace(guess, " "):
print(“You got the second guess right too!”)
else :
print(“test run1”)
else:
print(“I’m sorry that’s not in the word”)
counts += 1
else :
print(“Sorry, you don’t have anymore chances left. Play again”)

Readers can’t untangle that. Nor can an IDE unaided. In readable code, one can often spot the errors without running it.

Did I guess right?

3 Likes

Apart from various chores I’m sure you have like the rest of us … once it runs, it probably won’t do what you want. I suggest you learn to use the debugger to step through your code. You may figure out the problem yourself that way, and anyway it helps you think like a computer, which helps you write code that works.

pdb is part of Python but I find it quite painful to see where I am and the values of variables. Most IDEs provide a visual debugger that solves those two problems but is different in each IDE.

Guys, this is how you post code right, you use 3 backticks right?

Yes.

this code shows the error

We do the 3-backticks thing with tracebacks, too.

 File "/Users/mylaptop/Desktop/python folder/hangman.py", line 15
     else:
 IndentationError: expected an indented block after 'else' statement on line 14

It looks ok to me, and runs here with no traceback.

I suspect this is not exactly how the code was when you ran it, and
that you had one of the elses slightly misaligned.

Because Python uses indentation to express program structure, getting
these correctly aligned is essential.

On the other hand, you can put a call to the breakpoint() function in
your code to inspect things. Example:

 ... do your stuff ...
 if ... some bad value you know to expect ...:
     print("aha!")
     breakpoint()
 ... more code ...

That will drop you into the debugger at that point in the code with the
local variables available. Type “c” (continue) to resume the script.

You don’t need the if-statement, but I often find it annoying to drop
into the debugger on, say, every loop iteration. Using an if (if you
have a situation in mind) lets you drop into the debugging only when it
occurs.

Yes, I do this in circumstances where I’m unable to (or figure out how to) use the IDE, for example when the problem is in a subprocess or in the middle of tests orchestrated by another tool. @hmmmmmmmmmmmm : it is worth knowing how to use pdb as well as the IDE debugger.

if you are a beginner start using the IDE and learn the basics first and then learning these code will be easy . Good you are hitting the issues , you should start solving by yourself .