当我尝试运行代码时,它会不断重复,"到目前为止,您已经猜到了#34;当我只想重复一次。猜测时名称的输出应该是:secret_word = hello,然后在每个单词之后它应该是h ??? o或h?ll?
def get_secret_word():
while True:
secret_word = input('Please enter a word to be guessed\nthat does not contain ? or whitespace:')
if not ('?' in secret_word or ' ' in secret_word or secret_word == ''):
return secret_word
def is_game_over(wrong_guess, secret_word, output, chars_guessed):
if wrong_guess == 7:
print('You failed to guess the secret word:', secret_word)
return True
for guess in secret_word:
if guess in chars_guessed:
output += guess
else:
output += '?'
if not ('?' in output):
print('You correctly guessed the secret word:', secret_word)
return True
else:
return False
def display_hangman(wrong_guess):
if wrong_guess == 1:
print('\n |')
elif wrong_guess == 2:
print('\n |', '\n O')
elif wrong_guess == 3:
print('\n |', '\n O', '\n |')
elif wrong_guess == 4:
print('\n |', '\n O', '\n/|')
elif wrong_guess == 5:
print('\n |', '\n O', '\n/|\\')
elif wrong_guess == 6:
print('\n |', '\n O', '\n/|\\', '\n/')
elif wrong_guess == 7:
print('\n |', '\n O', '\n/|\\', '\n/', '\\')
def display_guess(secret_word, chars_guessed):
output = ''
for guess in secret_word:
if guess in chars_guessed:
output += guess
else:
output += '\nSo far you have guessed: '
for guess in chars_guessed:
output += guess + ","
print(output.strip(","))
def get_guess(secret_word, chars_guessed):
while True:
guess_character = input('Please enter your next guess: ')
if guess_character == '':
print('You must enter a guess.')
continue
elif len(guess_character) > 1:
print('You can only guess a single character.')
elif guess_character in chars_guessed:
print('You already guessed the character:', guess_character)
else:
return guess_character
def main():
wrong_guess = 0
chars_guessed = []
secret_word = get_secret_word()
output = ''
while not is_game_over(wrong_guess, secret_word, output, chars_guessed):
display_hangman(wrong_guess)
display_guess(secret_word, chars_guessed)
guess_character = get_guess(secret_word, chars_guessed)
chars_guessed.append(guess_character)
chars_guessed.sort()
if not guess_character in secret_word:
wrong_guess += 1
return wrong_guess
pass
if __name__ == '__main__':
main()
答案 0 :(得分:0)
所以我能看到的问题是:
缩进问题(正如其他一些海报所指出的那样):
- 带有if语句的get_secret_word()函数
- display_hangman()函数与所有elif的
还使用主函数
中的以下代码 if not guess_character in secret_word:
wrong_guess += 1
return wrong_guess
pass
这个退出了我的应用程序,据我所知,返回和传递行不需要。当做出错误猜测时, return 退出main函数,因此 pass 永远不会到达,因为 wrong_guess 是"全球" main函数中的变量新函数可用于函数中所有函数的活动时间长度。
对于重复循环,显示用户猜测它是你在display_guess()
函数中循环的方式。在被猜测的字符不在秘密字中的情况下,它在所有内容上循环两次,一次用于外部for循环,然后再用于内部循环。
我认为您应该将 display_guess()更改为如下所示。这样,用户将始终看到他们之前猜到的内容
def display_guess():
output = "\nSo far you have guessed: "
count = 0
if len(chars_guessed) > 0:
for guess in chars_guessed:
if count < len(chars_guessed) and count != 0:
output += ','
output += guess
count +=1
print output
至于显示带有猜测字符的秘密字,其余部分显示为?&#39>,您似乎无法打印输出变量的值。 is_game_over()功能随处可见。我还没有对它进行过测试,但在循环之后添加一个打印应该排除它。
在旁注上你可能希望在用户输入上添加验证,因为它除了数字和特殊字符