Making Hangman (Without the Stick Figures) 05-08-2014, 11:02 AM
#1
Before you ask 'Duubz, you didn't teach me anything! It's just code!', read the comments inside the code. THat's where the tutorial's at. I'm trying something different. Full commented code tutorials.
Code:
#Programmed by Duubz
#Https://www.sinister.ly/User-Duubz
#Sinisterly; The world's #! tech forum.
import random,sys
#FUCK YOU PYTHON2
if sys.version_info[0] > 2: raw_input = input
else: input = raw_input
#We need to have a set of words.
#For the sake of simplisity, we will only have one word ok for this tutorial.
word = 'python'
#We need a maximum number of tries, and we need to count how many the user has
#missed so far. We will set this to 0, as it will be changed later if they
#choose a letter that isn't in the word.
wrong,tries = 10,0 #We're going to make wrong 10, since this is our max.
#Now we will need a list of the letters they've used so far.
#Currently, it's empty (obviously), however more will be added when they
#play the game.
used_letters = []
#Now we will have a variable, 'won', to check if they've beaten the game.
#If they haven't, the game will continue. Otherwise, it will end..
#Also, we need to have the phrase that they spell out, hypothetically speaking
#if the user actually does get a letter right.
won,phrase = False,""
#Now we will start the actual game. There is really no need for definitions,
#however, some people like to add them. For this type of game, it's best not to
#go with defining anything because it will just get too repetitive, and we will
#have too many repeating codes. Thus, we will use a while statement.
print('_' * len(word)) #Shows length of word.
while(won != True): #Prints the status each time.
print('TRIES {0}/{1} - PHRASE {2} - USED {3}'.format(str(tries),
str(wrong),
str(phrase),
str(', '.join(used_letters))))
#We're going to get the user input. This will be one letter each time.
x = raw_input('> ') #Yes, we are using Python3
x = x.lower()
#use.
if x in list(word): #Assuming we've chosen our word, we are checking to
#see uf 'x', or our letter, is in list(word). The list function will
#split all letters.
phrase += x #This adds our letter to the phrase/word.
#Our tries won't go up, since we got it right in this case.
if x in used_letters: #This just disallows them from using a common character
print('You can\'t use {0} more than once!'.format(x)) #More than once.
x = raw_input('> ')
x= x.lower()
if x not in list(word):
tries += 1
used_letters.append(x)
if int(tries) == int(wrong): #This will be executed if they run out of
print('You lose!') #tries.
exit()
for c in list(phrase): #If they win. IF.
if (c in word)and(len(word) == len(phrase)):
print('You won!')
won = True
exit()
#Now, just save your game as hangman.py on your desktop, and play it.
#If it works great. If not, there's probably an error in your code somewhere.![[Image: BXqGARG.png]](https://i.imgur.com/BXqGARG.png)










![[+]](https://sinister.li/images/modern/collapse_collapsed.png)




![[Image: TeusoI9.png]](http://i.imgur.com/TeusoI9.png)




![[Image: CDUAq9d.png]](http://i.imgur.com/CDUAq9d.png)

![[Image: lO8j80T.gif]](https://i.imgur.com/lO8j80T.gif)