[英]Hangman game problems in Python
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.'
到目前为止,一旦调用一次游戏,当我输入一个字母时,它就只会继续打印“剩余的8个生命:”。几个字母之后,它只会打印出“您赢了”。 I am having trouble finding my mistakes and any help from python experts would be appreciated.
我无法发现自己的错误,因此python专家的任何帮助将不胜感激。
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. 您将忽略作为参数传入的
word
,并遍历显示为空的display_word
。 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. 缩进的方式,它总是在循环的第一次迭代中返回true或false。 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. 因此,只有在整个循环结束后才
return True
。
Or better still: 还是更好:
def all_letters_guessed(word):
return all(c.lower() in guessed_letters for c in word)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.