Skip to content

Instantly share code, notes, and snippets.

@xodene
Last active March 20, 2018 16:54
Show Gist options
  • Save xodene/68c0913402aabc340da687c90c649da5 to your computer and use it in GitHub Desktop.
Save xodene/68c0913402aabc340da687c90c649da5 to your computer and use it in GitHub Desktop.
Simple Hangman Game in Python. Written as a beginner exercise for the CGCC Coding Club.
word = "homecoming"
word_progress = ""
for i in range(0, len(word)):
word_progress += "_"
def update_solution(index, c):
#convert the string to a list
char_list = list(word_progress)
char_list[index] = c
#convert the list back into a string
return "".join(char_list)
#keep prompting user until they get the word right
while True:
print "Guess the word: " + word_progress + " "+ str(len(word)) + " letters"
c = raw_input("Enter a letter")
c = c.lower()
occurance = word.find(c)
count_occurances = 0
if occurance >= 0:
char_position = 0
sub = word[:len(word)]
word_progress = update_solution(occurance, c)
while occurance != -1:
sub = sub[occurance+1:]
occurance = sub.find(c)
char_position = word.find(sub) + occurance
word_progress = update_solution(char_position, c)
count_occurances = count_occurances+1
print "The word occurs: " + str(count_occurances) + " times"
else:
print "Wrong guess!"
if word_progress == word:
print "You've got it!"
break #exit the infinite loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment