当我在另一个函数上定义python时,为什么我会得到Name is not defined error?

2024-04-19 21:04:09 发布

您现在位置:Python中文网/ 问答频道 /正文

为什么我会得到名称未定义错误,如下面“for character in SECRET_WORD:”行所示,即使在前面我创建的“def category”函数中,我将SECRET_WORD定义为SECRET_WORD=random.choice(单词列表)

  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()

Tags: thetoinforsecretresponserandomlist
1条回答
网友
1楼 · 发布于 2024-04-19 21:04:09

您可以这样做:

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()

相关问题 更多 >