import random
def main():
num_guesses = 4
instruction_file=open('instructions.txt', 'r')
list_of_words = ['apple', 'banana', 'watermelon', 'kiwi', 'pineapple', 'mango']
answer=random.choice(list_of_words)
puzzle=['_'] * len(answer)
def display_instructions(instruction_file):
file_contents=instruction_file.read()
instruction_file=instruction_file.close()
print(file_contents)
def get_guess(num_guesses):
print('The number of guesses remaining is ' + str(num_guesses)+ '.')
letter_input = input("Guess a letter ")
return letter_input
def update_puzzle_string(letter_input,puzzle,answer):
if get_guess(num_guesses) in answer:
for i,x in enumerate(answer):
if x is get_guess:
puzzle[i]=letter_input
return True
def display_puzzle_string(puzzle):
print('The current state of the puzzle is '+str(puzzle))
def is_word_found(puzzle,answer):
is_word_found=True
puzzle_string=print(''.join(puzzle))
if puzzle_string == answer:
return False
def play_game(answer,puzzle):
while True:
display_puzzle_string(puzzle)
get_guess(num_guesses)
update_puzzle_string(get_guess,puzzle,answer)
print(str(puzzle))
is_word_found(puzzle,answer)
display_instructions(instruction_file)
play_game(answer,puzzle)
main()很抱歉出现格式化问题。这个程序的目标是收集用户的猜测,然后将其与列表中随机选择的单词进行比较,这样做后,它会更新一个拼图,其中包含字母所属的空格,如果猜到了单词的所有字母,用户就会被告知它们是正确的。用户得到4次猜测。它基本上就是一个绞刑者。当我执行这个程序时,它只是打印指令,初始的拼图状态,请求猜测,然后一直要求猜测。我不明白为什么这不管用。在获得帮助后,我将实现猜测的次数。
发布于 2019-10-09 22:53:25
import random
def main():
num_guesses = 4
instruction_file=open('instructions.txt', 'r')
list_of_words = ['apple', 'banana', 'watermelon', 'kiwi', 'pineapple', 'mango']
answer=random.choice(list_of_words)
puzzle=['_'] * len(answer)
def display_instructions(instruction_file):
file_contents=instruction_file.read()
instruction_file=instruction_file.close()
print(file_contents)
def get_guess(num_guesses):
print('The number of guesses remaining is ' + str(num_guesses)+ '.')
letter_input = input("Guess a letter: ")
return letter_input
def update_puzzle_string(letter_input,puzzle,answer):
for i,x in enumerate(answer):
if x is letter_input:
puzzle[i]=letter_input
return
def display_puzzle_string(puzzle):
print('The current state of the puzzle is '+str(puzzle))
def is_word_found(puzzle,answer):
is_word_found=True
puzzle_string=print(''.join(puzzle))
if puzzle_string == answer:
return False
def play_game(answer,puzzle):
while True:
display_puzzle_string(puzzle) #display '_ _ _ _ _'
guess = get_guess(num_guesses)
update_puzzle_string(guess,puzzle,answer)
#print(str(puzzle)) #this statement is causing the repetitive puzzle prints
is_word_found(puzzle,answer)
display_instructions(instruction_file)
play_game(answer,puzzle)
main()在修复了格式化问题之后,代码现在反复请求猜测并接受输入。我认为问题主要存在于格式上,因为我现在没有得到错误。另外,你之前有没有随机导入?干杯
编辑:
请参阅对update_puzzle_string()和play_game的更改。您反复调用get_guess函数,而不是使用它的初始返回值。

编辑2:(请参考此答案的注释)
有关“答案中相同字母的倍数”的更改,请参见update_puzzle_string()

发布于 2019-10-09 22:59:48
所以你的错误是不能停止软件?
您可以在play_game中使用is_word_found,如果找到则中断。
如下所示:
def play_game(answer,puzzle):
while True:
display_puzzle_string(puzzle)
get_guess(num_guesses)
update_puzzle_string(get_guess,puzzle,answer)
if is_word_found(puzzle, answer):
print("You won!")
break
print(str(puzzle))Break将停止while True无限循环
https://stackoverflow.com/questions/58306650
复制相似问题