Created
February 27, 2018 03:56
-
-
Save bgschiller/5a3d3e138a60f3e307514daa97ca3a80 to your computer and use it in GitHub Desktop.
hangman game made on 2018-02-26 at DPL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
try: | |
input = raw_input | |
except NameError: | |
pass | |
def replace_at(the_word, pos, letter): | |
""" | |
produce a new string, just like <the_word>, | |
except the letter at <pos> has been | |
changed to <letter> | |
""" | |
return ( | |
the_word[:pos] + # everything up to pos | |
letter + | |
the_word[pos + 1:] # everything after pos | |
) | |
""" | |
Welcome to hangman! | |
You have 6 wrong guesses left. | |
_ _ _ _ _ _ _ _ | |
your guess: e | |
Correct! | |
e _ e _ _ _ _ _ | |
your guess: z | |
uh oh... | |
You have 5 wrong guesses left. | |
e _ e _ _ _ _ _ | |
your guess: | |
""" | |
print('Welcome to hangman!') | |
word = 'elephant' | |
word_so_far = '________' | |
wrong_guesses = 6 | |
while word != word_so_far: | |
print('You have', wrong_guesses, 'wrong guesses left') | |
# print out as many _s as there are letters in | |
# our word. | |
for letter in word_so_far: | |
print(letter, end=' ') | |
print() # just put the newline | |
# please print out 'your guess: ' and then | |
# collect some input from the user. store the | |
# input in the variable 'guess' | |
guess = input('your guess: ') | |
if guess == '': | |
print('please make a guess') | |
elif guess in word: | |
print('Correct!') | |
# every location 'e' appears in word, we need to | |
# reveal it in word_so_far | |
for pos in range(len(word)): | |
if word[pos] == guess: | |
word_so_far = replace_at(word_so_far, pos, guess) | |
else: | |
print('uh oh...') | |
# subtract one from wrong_guesses | |
wrong_guesses -= 1 | |
if wrong_guesses == 0: | |
print("you're out of guesses!") | |
# now, get OUT OF THE LOOP | |
break | |
if word == word_so_far: | |
print ('nice work!') | |
else: | |
print ('the word was', word) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment