Python 为什么赢了';当我调用它时,我的功能没有激活吗?
很抱歉出现格式问题。这个程序的目标是收集用户的猜测,然后将其与从列表中随机选择的单词进行比较。这样做后,它会更新一个拼图,其中字母所属的位置是空白的。如果猜到了单词的所有字母,则会告诉用户它们是正确的。用户得到4次猜测。基本上是刽子手。当我执行这个程序时,它只是打印指令,初始的谜题状态,请求猜测,然后继续要求猜测。我不明白这为什么不起作用。在获得帮助后,我将实现猜测次数Python 为什么赢了';当我调用它时,我的功能没有激活吗?,python,function,Python,Function,很抱歉出现格式问题。这个程序的目标是收集用户的猜测,然后将其与从列表中随机选择的单词进行比较。这样做后,它会更新一个拼图,其中字母所属的位置是空白的。如果猜到了单词的所有字母,则会告诉用户它们是正确的。用户得到4次猜测。基本上是刽子手。当我执行这个程序时,它只是打印指令,初始的谜题状态,请求猜测,然后继续要求猜测。我不明白这为什么不起作用。在获得帮助后,我将实现猜测次数 import random def main(): num_guesses = 4 instruction_f
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()
在修复了格式问题后,代码现在会反复询问猜测并接受输入。我认为这个问题主要存在于格式方面,因为我现在没有错误。还有,你以前有没有随机导入过?干杯
编辑:
查看对更新拼图字符串()和玩游戏的更改。您反复调用get_guess函数,而不是使用其初始返回值。
编辑2:(参考本答案的评论)
有关“答案中相同字母的倍数”的更改,请参见update\u puzzle\u string()
在修复了格式问题后,代码现在会反复询问猜测并接受输入。我认为这个问题主要存在于格式方面,因为我现在没有错误。还有,你以前有没有随机导入过?干杯
编辑:
查看对更新拼图字符串()和玩游戏的更改。您反复调用get_guess函数,而不是使用其初始返回值。
编辑2:(参考本答案的评论)
有关“答案中相同字母的倍数”的更改,请参见update\u puzzle\u string()
那么您的错误是无法停止软件
你可以使用在游戏中找到的单词,如果找到了就打断
像这样:
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()
Break将停止,而True
无限循环,因此您的错误无法停止软件
你可以使用在游戏中找到的单词,如果找到了就打断
像这样:
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()
Break将停止,而True
无限循环您的代码存在严重的缩进问题,这使得Python很难理解。你能纠正一下吗?只需编辑您的问题,粘贴代码,选择它,然后使用{}
按钮将其格式化为代码。不要对大代码块使用反勾号。@FredLarson完成,谢谢!当我运行此代码时(在顶部添加import random
后),它会不断请求输入。@JohnGordon这是我的问题,我无法让它停止猜测。编程时,你应该采取更“一步一步”的方法,代码中似乎有很多东西都是针对基函数的,这些基函数并没有完成您期望它们完成的任务。还有就是你把一些函数和变量搞混了,而且你没有使用函数返回的内容。你的代码有严重的缩进问题,这使得Python很难理解。你能纠正一下吗?只需编辑您的问题,粘贴代码,选择它,然后使用{}
按钮将其格式化为代码。不要对大代码块使用反勾号。@FredLarson完成,谢谢!当我运行此代码时(在顶部添加import random
后),它会不断请求输入。@JohnGordon这是我的问题,我无法让它停止猜测。编程时,你应该采取更“一步一步”的方法,代码中似乎有很多东西都是针对基函数的,这些基函数并没有完成您期望它们完成的任务。还有就是你把一些函数和变量混淆了,而且你没有使用函数返回的内容。哦,很抱歉,我的意思是它一直在询问猜测,而不是用猜测更新拼图字符串。就像它只是一次又一次地询问猜测,而不是继续更新拼图或打印更新的拼图。非常感谢!“我太迷路了,真是难以置信,你是个救命恩人。”马蒂索哈德尔玛:没问题,很高兴我能帮上忙!谢谢你接受答案:)与原来的问题无关,但是为什么猜测没有填入单词的所有适当字母呢。例如,西瓜的猜测e只填充了其中一个e。@Mathissohardlmao请看我关于代码更改的更新答案。哦,对不起,我的意思是它一直要求猜测,而不是用猜测更新拼图字符串。就像它只是一次又一次地询问猜测,而不是继续更新拼图或打印更新的拼图。非常感谢!“我太迷路了,真是难以置信,你是个救命恩人。”马蒂索哈德尔玛:没问题,很高兴我能帮上忙!谢谢你接受答案:)与原来的问题无关,但是为什么猜测没有填入单词的所有适当字母呢。例如,西瓜的猜测e只填充了e中的一个。@Mathissohardlmao请参阅我关于代码更改的最新答案。他的最初问题是通过反复调用函数而创建的循环,而不是将函数的结果保存到变量中并使用它。他的最初问题是通过反复调用函数创建的循环而不是将函数的结果保存到变量中并使用该变量。