First, some comments on the UI:
The word is t e s t * n *
What is your guess? g
You got one right! no hangman for you this turn
The word is t e s t * n g
What is your guess? i
You guessed it right before getting a hangman! You win!
The word was testing
You need to do a better job separating the "game board" from the verbiage. Also, maybe put some quotes and other punctuation around the printed answer:
The word was, "testing"!
Now for the code. I'm not going to do a complete review because ... it makes my eyes burn. Instead, I'm going to assume that:
- You're coming to python from C or C# or Java - some language with array support and lots of curly braces.
- You're in a course, and being deliberately held back.
So let's look at your coding style:
import random
print('let\'s play hangman')
#set words
first = ['t','e','s','t']
second = ['t', 'e', 's', 't', 'e', 'd']
third = ['t', 'e', 's', 't', 'i', 'n', 'g']
fourth = ['r', 'a', 'n', 'd', 'o', 'm']
fifth = ['g', 'e', 'n', 'e', 'r', 'a', 't', 'e']
#set list of possible answers
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
#set a couple of varibles
Use vertical space to separate unrelated things
You didn't separate things using vertical space. This is perhaps the greatest sin in programming. Always, always use vertical space (blank lines, paragraphs, etc.) to separate parts of your code. If you switch from doing one thing to another, insert a blank line. If you end a paragraph and start a new paragraph, insert a blank line. If you come to the end of a function or a loop, insert a blank line.
Even if you are the most secretive hacker ever, and you never intend to share your code with a single other living human being, at some point you will have to go back and read stuff that you, yourself wrote six months earlier! Make it easy on yourself, or on whatever other humans have to read your code: try to make it easy to read! Use vertical space to separate unrelated things.
Keep related things together
Next, consider the line:
print('let\'s play hangman')
Ignoring the lack of a leading capital letter, and no ending punctuation, why is this line where it is?
This is clearly the "start of the game." That's fine. But at line #2, you aren't ready to start the game. There's a bunch of variable initialization and other things that need to happen before the game starts. This line should be moved down to where all the rest of the user-facing code is located. Keep related things together.
Let your code speak for itself
Your comments suck.
There are six basic questions you can answer: Who, What, Where, When, Why, and How?
Of those, three are only used in intro-to-programming courses: Who, Where, When. If you have to put a big header block at the top of your program with your student id, name, course number, professor's name, etc., that's okay. Just do it once, and we will all skip right over it!
The other three- What, Why, and How- should only be used if you can't actually answer the question with the code itself!
Specifically, if you can't choose your variable, type, and subroutine names in such a way as to make the answer obvious.
If you can make the answer obvious, then don't add a comment. It just wastes my time, and annoys the compiler.
To wit:
#set varible length to the number of letters in the word
length = len(words)
First, you misspelled "variable." But second: how could you possibly think this comment adds value to your source code?
There are some occasions when you need to answer the question "What?" But those occasions are few and far between. You're actually better off never answering that question unless somebody calls you on it. Until and unless someone asks you "what is this code doing?" I would suggest you simply never use a comment to explain what you are doing - because at the "learning" stage, it is almost always obvious.
You might explain "why" you're computing some value, or "how" you're doing a computation. But, again, for the most part these things are obvious.
Now consider your variable, words:
first = ['t','e','s','t']
# ...
random=random.randint(1, 5)
if random == 1: words = first
Apparently, words is actually the secret word the player is trying to guess. Which is fine, except the whole program is about words, and words is plural - suggesting perhaps a list of words or a dictionary of words or a disk file containing more than one actual word!
This variable is the most important piece of data in your program, and you named it wrong. Maybe secret_word instead? (Because answer is taken.)
There are very few beginner programs that need comments. But there are often programs that need better variable or function names. Let your code speak for itself.
K.I.S.S.
Consider this:
for loop in range(length):
end = end + loop
# ...
while wrong < 5 and endCheck != end:
# ...
for marker2 in range(length):
if words[marker2] == letter:
# ...
endCheck = endCheck + marker2
This is "interesting" because it appears that you determine that the player has won the game by adding up the index of all the correctly guessed letters, 0 + 1 + 2 + 3 + 4 ... and matching the sum against a pre-computed value.
This is so bizarre that I wonder if you copied your solution from a different language. Perhaps in C, or Pascal, or assembly, or just any language with bad string support? Also, of course, it won't work: you start with zero, and you use 0 as the index of the first character. This is a bug.
The word is * e n e r a * e
What is your guess? t
You guessed it right before getting a hangman! You win!
The word was *enerate
Ignoring the whole "it doesn't work" thing, this solution is vulnerable to all sorts of programmer errors, and it is not at all obvious what you're doing.
A far better test for "done" would be simple equality: is A equal to B? It turns out that Python implements list equality correctly. You can simply say,
if listA == listB:
and get the correct result. Alternatively, you could store your words as strings rather than lists. This would make it somewhat awkward to manipulate them, since Python strings are immutable. But it would again let you simply say,
if wordA == wordB:
or even,
while wordA != wordB:
and have a simple, obvious operation for checking if the player has won.
As the saying goes, "keep it simple stupid."
A place for everything...
Most of your code is inside a while loop. But much of your code doesn't get executed more than once!
Consider this:
#check if entire word has been guessed
if endCheck == end:
print('You guessed it right before getting a hangman! You win!')
print ('The word was ', end = '')
for word in range(length):
print(answer[word], end='')
#check if a right letter was guessed this turn
if right > 0 and endCheck != end:
print('You got one right! no hangman for you this turn')
#check if a wrong letter was guessed this turn
if right == 0:
print('You missed it this turn!')
wrong = wrong + 1
print ('number missed =', wrong)
#check if to many wrong guesses were made
if wrong == 5:
print('You got a hangman!')
That code is a mixture of "things to do at the end of the game" and "things to do in a loop."
Mixing things like that is a mistake. First, because it requires you to check your conditions over and over (which is inefficient). And second because you are flipping back and forth: he's in! he's out! he's in! he's out! This is unnecessary complexity (violating the KISS principle), and it doesn't keep related things together.
Instead of jumbling all these conditions together, ask yourself: what things go inside the loop, and what things go outside the loop?
Obviously, winning or losing the game is "outside the loop" because in either case you won't be asking for any more guesses.
Similarly, reporting if a single guess was right or wrong is inside the loop, because then you will ask for more guesses.
Consider using the break or the continue statement to short-circuit the rest of the loop body. Like this:
while True:
# guess, etc.
if answer == secret_word or wrong_guesses == 5:
break
if answer == secret_word:
# you win
else:
# you lose
Identify the various conditions, and then have "A place for everything, and everything in its place!"
I'm not going to address the places where your code is not "Pythonic". You've said you're learning, and I believe you. Plus, I expect some other people will chime in for that. There are a lot of things you can do to clean this code up, and to make better use of language features. But I think you should address the "bigger" issues first, because they will still be true even if you aren't programming in Python. Bad comments in C# are still bad comments. Poor variable names in Scheme are still poor variable names.