Reputation: 53
Here is my complete code:
import random
guessesMade = 0
lives_remaining = 8
roundsPlayed = 0
roundsWon = 0
guessed_letters = ''
words = ['chicken', 'dog', 'cat', 'mouse', 'frog']
def pick_a_word():
word_position = random.randint(0, len(words) - 1)
return words[word_position]
print(pick_a_word())
def play():
print('Welcome to hangman! The rules are simple: A word will be chosen at random and will be represented by a sequence of blanks. Each blank constitutes a letter in the word. You will be asked to enter a letter and if the letter is contained in the word you will be notified. You can only make an incorrect guess 8 times before you lose the round. To win the round you must guess all the letters and reveal the word. Good luck!\n\n')
word = pick_a_word()
while True:
guess = get_guess(word)
if process_guess(guess,word):
print('You won.')
break
elif lives_remaining == 0:
print("\nI'm sorry, but you have run out of guesses. The word was {}.".format(randWord))
break
def get_guess(word):
print_word_with_blanks(word)
print('Lives Remaining: ' + str(lives_remaining))
guess = input('Guess a letter or the word if you know it.')
return guess
def print_word_with_blanks(word):
display_word=''
for letter in display_word:
if guessed_letters.find(letter) > -1:
display_word = display_word + letter
else:
display_word = display_word + '-'
print(display_word)
def process_guess(guess,word):
if len(guess) > 1 and len(guess == len(word)):
return whole_word_guess(guess,word)
else:
return single_letter_guess(guess, word)
def whole_word_guess(guess,word):
global guessesLeft
if guess.lower() == word.lower():
return True
else:
guessesLeft -=1
return False
def single_letter_guess(guess,word):
global guessed_letters
global lives_remaining
if word.find(guess) == -1:
lives_remaining -= 0
guessed_letters = guessed_letters + guess.lower()
if all_letters_guessed(word):
return True
return False
def all_letters_guessed(word):
for letter in word:
if guessed_letters.find(letter.lower()) == -1:
return False
return True
play()
Sorry for the big code block, but I wanted to provide a look at the full program to help with fixing my mistakes. As of now once play is called it just keeps printing out 'Lives remaining 8:' when I enter a single letter in. After a few letters it just prints out 'You won.' I am having trouble finding my mistakes and any help from python experts would be appreciated.
Upvotes: 0
Views: 454
Reputation: 59112
In this function:
def print_word_with_blanks(word):
display_word=''
for letter in display_word:
if guessed_letters.find(letter) > -1:
display_word = display_word + letter
else:
display_word = display_word + '-'
print(display_word)
You are ignoring the word
passed in as a parameter, and looping through display_word
, which is empty. So the actual word (with spaces) is never printed at all.
Presumably you mean to say:
for letter in word:
...
Or you can write it a little more concisely:
def print_word_with_blanks(word):
print(''.join(c if c in guessed_letters else '-' for c in word))
Another problem you are having is here:
def all_letters_guessed(word):
for letter in word:
if guessed_letters.find(letter.lower()) == -1:
return False
return True
The way this is indented, it always returns true or false on the first iteration of the loop. It should be like this:
def all_letters_guessed(word):
for letter in word:
if guessed_letters.find(letter.lower()) == -1:
return False
return True
so the return True
only fires after the whole loop has finished.
Or better still:
def all_letters_guessed(word):
return all(c.lower() in guessed_letters for c in word)
Upvotes: 1