python - 当我在另一个函数上定义它时,为什么我会得到 Python 的 Name is not defined 错误?
问题描述
为什么我得到名称未定义错误,如下所示,在“用于 SECRET_WORD 中的字符:”行中,即使早些时候在我创建的“def 类别”函数中,我将 SECRET_WORD 定义为 SECRET_WORD = random.choice(word_list )?
import random
# Store the category and values into a dictionary
categories = {
"objects": ["tables", "ladders", "chairs"],
"animals": ["chicken", "dog", "cat"],
"sports": ["basketball", "soccer", "rugby"]
}
def category():
print("Please enter category name: ")
response = ''
#Keep prompting the user to only enter allowed category values
while response.lower() not in categories:
# join(map(str, list((*categories,))) is used for retrieving the key values i.e. the category values from the dictionary "categories" and then join them as a string in order to display the allowed values back to the user
response = input(' One among the following [%s] : \n' % ', '.join(map(str, list((*categories,)))))
if response in categories:
word_list = categories.get(response)
# Print a random value from the chosen category
print(random.choice(word_list)
SECRET_WORD = random.choice(word_list)
LENGTH_WORD = len(SECRET_WORD)
GUESS_WORD = []
ALPHABET = "abcdefghijklmnopqrstuvwxyz"
letter_storage = []
def prepare_secret_word() -> None:
"""Prepare secret word and inform user of it"""
for character in SECRET_WORD: # <---------------- Name "SECRET_WORD" not defined error here"
GUESS_WORD.append("-")
print("Ok, so the word You need to guess has", LENGTH_WORD, "characters")
print("Be aware that You can enter only 1 letter from a-z\n\n")
print_word_to_guess(GUESS_WORD)
# Call the function
category()
prepare_secret_word()
用我的最新代码更新了更改(仍然有错误),如下所示
import random
category_lists = {
"objects": ["tables", "ladders", "chairs"],
"animals": ["chicken", "dog", "cat"],
"sports": ["basketball", "soccer", "rugby"]
}
def category():
print("Please enter category name: ")
response = ''
while response.lower() not in category_lists:
# join(map(str, list((*categories,))) is used for retrieving the key values i.e. the category values from the dictionary "categories" and then join them as a string in order to display the allowed values back to the user
response = input(' One among the following [%s] : \n' % ', '.join(map(str, list((*category_lists,)))))
if response in category_lists:
word_list = category_lists.get(response)
# do what ever you want with the list
SECRET_WORD = random.choice(word_list)
LENGTH_WORD = len(SECRET_WORD)
return SECRET_WORD
return LENGTH_WORD
GUESS_WORD = []
ALPHABET = "abcdefghijklmnopqrstuvwxyz"
letter_storage = []
def prepare_secret_word() -> None:
"""Prepare secret word and inform user of it"""
SECRET_WORD = category()
LENGTH_WORD = category()
for character in SECRET_WORD: # printing blanks for each letter in secret word
GUESS_WORD.append("-")
print("Ok, so the word You need to guess has", LENGTH_WORD, "characters")
print("Be aware that You can enter only 1 letter from a-z\n\n")
category()
prepare_secret_word()
解决方案
你可以这样做:
def category():
if response in categories:
SECRET_WORD = random.choice(word_list)
else: # define else result
SECRET_WORD = ''
return SECRET_WORD
def prepare_secret_word():
# get variable from function
SECRET_WORD = category()
for character in SECRET_WORD:
#####
# run
prepare_secret_word()
推荐阅读
- python - 如何保存 PyTorch 模型,而无需导入用于创建它的模块?
- html - 为什么另一个 div/class 中的 div/class 只继承某些属性?
- javascript - 我不明白为什么它返回未定义
- hugo - 在 Hugo 中将单个文件拆分为多个“帖子”?
- powerbi - 在 powerBI 中复制 spotfire 查询
- html - 如果我的 html 视频既不是 youtube 链接(例如在这种情况下),也不是我使用 mp4,为什么我的 html 视频不起作用?
- javascript - jquery Validate Casuing Initail 无效元素在第二次“保存”按钮单击时变为有效
- javascript - 音频未在渲染时播放 - React
- algorithm - Levenshtein Word Distance 和 Boyer Moore 搜索算法有什么关系?
- python - 如何构建具有 2 组独立权重和 2 个损失的神经网络?