Почему я получаю имя, это не определенная ошибка, как показано ниже в строке "для символа в SECRET_WORD:", хотя ранее в функции "def category", которую я создал, я определил 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()