gpt4 book ai didi

python - 内置名称错误 : name 'guess' is not defined

转载 作者:太空宇宙 更新时间:2023-11-03 20:20:33 24 4
gpt4 key购买 nike

我正在做一项实验室作业,几天来我一直在与一个错误作斗争。我也在我的函数中编码了 return ,它一直说有一个 NameError: 但我没有定义它。

这是我正在编写的代码,由于我还没有完成,所以一切都搞砸了。但我很想知道我搞砸了什么以及如何修复名称错误。谢谢!

import random

def main():
instructions = display_instructions("instructions.txt")


#display instructions
display_instructions(instructions)


list_of_words = ['apple', 'banana', 'watermelon', 'kiwi', 'pineapple',
'mango']

correct_word=random.choice(list_of_words)
answer = list(correct_word)

puzzle = []
puzzle.extend(answer)

display_puzzle_string(puzzle)

play_game(puzzle, answer)

display_result(is_win, answer)


input('Press enter to end the game.')

def display_instructions(filename):
instruction_file=open("instructions.txt","r")
file_contents=instruction_file.read()
instruction_file.close()
print(file_contents)


def display_puzzle_string(puzzle):
for i in range(len(puzzle)):
puzzle[i] = '_'
print('The answer so far is '+" ".join(puzzle))


def play_game(puzzle, answer):
num_guesses = 4
while num_guesses > 0:
get_guess(num_guesses)
update_puzzle_string(puzzle, answer, guess)
display_puzzle_string(puzzle)
is_word_found(puzzle)


def get_guess(num_guesses):
guess=input('Guess a letter '+'('+str(num_guesses)+' guesses remaining):')
return guess


def update_puzzle_string(puzzle, answer, guess):
for i in range(len(answer)):
if guess.lower() == answer[i]:
puzzle[i] = guess.lower()
num_guesses += 1
return puzzle


def is_word_found(puzzle):
if puzzle == answer:
return is_win


def display_result(is_win, answer):
if is_win:
print('Good job! You found the word '+correct_word+'!')

else:
print('Not quite, the correct word was '+correct_word+
'. Better luck next time')

main()

最佳答案

在函数 play_game 中,您有行 get_guess(num_guesses) 返回变量猜测。但是,您没有将猜测值分配给函数中的变量。您可以通过将该行更改为 guess = get_guess(num_guesses) 来解决此问题。该错误准确地告诉您出了什么问题。到目前为止,您还没有定义变量 guess

关于python - 内置名称错误 : name 'guess' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58188584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com