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:
If your IDE allows it, turn on display of âwhitespaceâ characters. (Now you might be able to see what is wrong.)
Set your IDE so TAB indents by 4 spaces.
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.
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?
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.
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.
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 .