Hangman Python重复函数
当我试图运行代码时,它不断重复,“到目前为止,你已经猜到了”,而我只希望它重复一次。当进行猜测时,名称的输出应该是这样的:secret\u word=hello,然后在每个单词后面应该是h???o或h?llHangman Python重复函数,python,if-statement,while-loop,function,Python,If Statement,While Loop,Function,当我试图运行代码时,它不断重复,“到目前为止,你已经猜到了”,而我只希望它重复一次。当进行猜测时,名称的输出应该是这样的:secret\u 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:')
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()
因此,我可以看到的问题是: 缩进问题(其他一些海报已经指出):
-使用if语句获取\u secret\u word()函数
-显示带有所有elif的函数 也与下面的代码位从主要功能
if not guess_character in secret_word:
wrong_guess += 1
return wrong_guess
pass
这退出了我的应用程序,就我所能看到的回报和通过线是不需要的。当做出错误的猜测时,返回退出主函数,因此永远不会达到通过,并且由于错误的猜测是主函数中的一个“全局”变量,因此在函数激活的时间长度内,新计数可用于该函数中的所有内容
对于显示用户猜测的重复循环,这是在display\u guess()
函数中循环的方式。如果猜到的字符不在秘密单词中,它会在所有内容上循环两次,一次用于外部for循环,然后再次用于内部循环
我想你应该把显示改成下面的样子。这样,用户将始终看到他们之前猜测的内容
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
def display_guess():
output=“\n您猜得太远了:”
计数=0
如果len(猜字符)>0:
对于猜字谜:
如果count
至于显示带有猜测字符的秘密单词,其余的显示为?,您似乎没有在任何地方打印is_game_over()函数的输出变量的值。我还没有测试过它,但是在for循环之后添加一个打印应该可以解决这个问题
另一方面,您可能希望在用户输入上添加验证,因为它不包括数字和特殊字符请修复代码中的缩进。我很确定,现在编写的代码只会无限次地要求输入新的密码,所以你的缩进还没有完全正确。唯一的问题是它没有显示到目前为止命名的猜测。谢谢。您在程序中看到的其他问题吗?除了最后一句中的建议之外,我想补充一下,可能所有输入都默认为小写,因为对于用户的猜测,H与H不同。还应该在sys.exit()中使用,以便脚本可以很好地退出。