I'm a noobie in programming so I've made a simple hangman game in python and I'd like someone to tell me how I could improve the code.
edit:How could I do it using classes?
from sys import exit
def makeGallow(gallow):
for i in range(8):
gallow.append([])
gallow[0].append(6 * '_')
gallow[1].append(' | |')
for i in range(2, 7):
gallow[i].append(' |')
gallow[7].append(11 * '-')
gallow = list()
secret_word = raw_input("Choose a word: ").lower()
secret_wordls = list()
letters = len(secret_word)
for c in secret_word:
secret_wordls.append(c)
blanks = list()
for i in range(len(secret_word)):
blanks.append('_')
lettersUsed = list()
tries = 0
makeGallow(gallow)
while True:
for i in gallow:
print "".join(i)
print "".join(i for i in blanks)
used = ", ".join(i for i in lettersUsed)
print "Letters used: " + used
letter = raw_input("Type a letter: ").lower()
if letter == 'quit':
exit(1)
elif letter in secret_word:
letters -= 1
if letters == 0:
print "You won!"
exit("Bye!")
else:
switch = secret_wordls.index(letter)
blanks[switch] = letter
elif letter not in secret_word:
print "Wrong!"
tries += 1
lettersUsed.append(letter)
if tries == 1:
gallow[2] = ' O |'
elif tries == 2:
gallow[3] = ' | |'
elif tries == 3:
gallow[3] = '/| |'
elif tries == 4:
gallow[3] = '/|\ |'
elif tries == 5:
gallow[4] = ' | |'
elif tries == 6:
gallow[5] = '/ |'
elif tries == 7:
gallow[5] = '/ \ |'
print "Sorry you lost :(."
print "The word was: " + secret_word
exit("Bye!")
elif letter in lettersUsed:
print "You have already typed that letter!"
print "Try another one."
edit2: I found a bug where if the work had 2, 3... times the same letter it would count as one and you couldn't win. I change the while loop:
while True:
for i in gallow:
print "".join(i)
print "".join(i for i in blanks)
used = ", ".join(i for i in lettersUsed)
print "Letters used: " + used
letter = raw_input("Type a letter: ").lower()
if letter == 'quit':
exit("Bye")
elif letter in secret_word:
for i in range(secret_wordls.count(letter)):
switch = secret_wordls.index(letter)
blanks[switch] = letter
secret_wordls[switch] = '-'
letters -= 1
elif letter not in secret_word:
print "Wrong!"
tries += 1
lettersUsed.append(letter)
if tries == 1:
gallow[2] = ' O |'
elif tries == 2:
gallow[3] = ' | |'
elif tries == 3:
gallow[3] = '/| |'
elif tries == 4:
gallow[3] = '/|\ |'
elif tries == 5:
gallow[4] = ' | |'
elif tries == 6:
gallow[5] = '/ |'
elif tries == 7:
gallow[5] = '/ \ |'
print "Sorry you lost :(."
print "The word was: " + secret_word
exit("Bye!")
elif letter in lettersUsed:
print "You have already typed that letter!"
print "Try another one."
if letters == 0:
print "You won!"
exit("Bye!")