我试图解决一个问题几个小时,但无法进一步。这是关于缺乏理解为什么某些变量的行为不符合我的预期。我想我将问题确定为被解释为字符串而不是变量的变量。
这是我的代码:
# Global variables.
fillers = ["__1__", "__2__", "__3__", "__4__"]
difficulties = ["easy", "medium", "hard"]
easy_text = "The Three Little Pigs is a fable about three __1__ who build three __2__ of different materials. A big bad __3__ blows down the first two pigs' houses, made of straw and sticks respectively, but is unable to destroy the third pig's house, made of __4__."
easy_answers = ["pigs", "houses", "wolf", "bricks"]
因此,在这种特殊情况下,我的问题围绕着变量easy_text和easy_answers的显示
# Function to be called to check if difficulty is available
def difficulty_level(difficulties, user_input):
for diff in difficulties:
if diff in user_input:
return diff
return None
# User sets a difficulty level and variables for further use are being created accordingly
def diff_validation():
user_input = raw_input("To do so enter either easy, medium or hard:")
difficulty = difficulty_level(difficulties, user_input)
if difficulty != None:
print "Your difficulty level was sucessfully set to " + difficulty + "!"
fl_text = difficulty + "_text"
fl_answers = difficulty + "_answers"
return fl_text, fl_answers
因此,我在这里根据所选的难度陈述fl_text和fl_answers。在我的例子中,它们变得easy_text和easy_answer
else:
print "Something went wrong please try again."
return diff_validation()
# Initialising the game with welcome text and difficulty selection.
print "Hello and welcome to a short little game testing your general knowledge. Dont be intimidated you can choose your own difficulty level."
fl_text, fl_answers = diff_validation()
print fl_answers
现在我无法理解我脑子里出了什么问题。如果我现在打印fl_answers我会得到"easy_answers",但我想打印列表。因此,为了保持动态,我不能只打印"easy_answers",它需要了解自己,我想打印easy_answers
列表。
随着我们在代码中的进一步,原因变得清晰起来:
def play_game(fl_string, fillers):
replaced = []
fl_string = fl_string.split()
for word in fl_string:
replacement = word_in_pos(word, fillers)
if replacement != None:
user_input = raw_input("Type in a: " + replacement + " ")
answer_nr = 0
if user_input == fl_answers[answer_nr]:
word = word.replace(replacement, user_input)
replaced.append(word)
answer_nr + 1
else:
print "Your answer was wrong please try again."
print fl_answers[1]
else:
replaced.append(word)
replaced = " ".join(replaced)
return replaced
print play_game(fl_text, fillers)
我尝试打印出fl_answers
的列表元素 1 以检查此元素是否与用户输入相同。但它确实给了我"easy_answers"的第二个字母。所以我发现不知何故我确实声明它应该是对列表的引用,而不是定义的变量。
也不是我希望代码的底线easy_text从fl_text中解脱出来,但它只是没有像编写fl_text那样执行它。如果我手动输入easy_text那么至少它会执行。程序本身的功能还没有发展到令我满意的程度。
我希望有人能理解我的问题,并帮助我理解我在这里忘记的基本原则。
您的函数diff_validation()
返回的是字符串而不是变量。因此,当您打印时,您应该打印字符串而不是列表。要从匹配的变量名称中获取列表变量,您可以使用如下字典:
# Global variables.
fillers = ["__1__", "__2__", "__3__", "__4__"]
difficulties = ["easy", "medium", "hard"]
easy_text = "The Three Little Pigs is a fable about three __1__ who build three __2__ of different materials. A big bad __3__ blows down the first two pigs' houses, made of straw and sticks respectively, but is unable to destroy the third pig's house, made of __4__."
easy_answers = ["pigs", "houses", "wolf", "bricks"]
lists = {"easy_text":easy_text, "easy_answers":easy_answers}
然后检索:
fl_text, fl_answers = diff_validation()
print lists[f1_answers]