This question already has an answer here:
So I know that the question I am about to ask is going to show just how green I am to Python. But I have hit a major wall and I am tired of error of messages (aren’t we all)
Anyhow, Just like I said I am new to Python, and have been just been working along with the book I have been using to learn the language. I just got done with the chapter covering functions and the final assignment was to program a game of "Hangman." Now I programmed this game to the best of my abilities but no matter how much I try I am still getting syntax errors. I have searched my code up and down, and I cannot find my syntax error…anywhere. Below I included my code if you can please look at it and tell me where I am going wrong that would be great:
import random
words = ['chicken', 'dog', 'cat', 'mouse', 'frog',]
lives_remaining = 14
guessed_letters = ''
def play() :
word = pick_a_word()
while True :
guess = get_guess (word)
if process_guess (guess, word) :
print ('you win, well done')
break
if lives_remaining == 0:
print ('you are hung!')
print ('The word was:' + word)
break
def pick_a_word() :
return random.choice(words)
def get_guess(word) :
print_word_with_blanks(word)
print ('Lives remaining: ' + str(lives_remaining))
guess = input('guess a single letter or the whole word')
return guess
def print_word_with_blanks(words) :
display_word = ''
for letter in words :
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:
return whole_word_guess (guess, word)
else :
return single_letter_guess (guess, word)
def whole_word_guess(guess, word) :
global lives_remaining
if guess == word:
return True
else:
lives_remaining = lives_remaining -1
return False
def single_letter_guess(guess, word) :
global guessed_letters
global lives_remaining
if word.find(guess) == -1:
lives_remaining = lives_remaining -1
guessed_letters = guessed_letters + guess
if all_letters_guessed (word):
return True
return False
def all_letters_guessed(word) :
for letter in word:
if guessed_letters.find(letter) == -1:
return True
Aucun commentaire:
Enregistrer un commentaire