Python中的字符串和变量问题
我花了几个小时试图解决一个问题,但没法再进一步了。这是因为缺乏理解为什么一些变量的行为方式与我预期的不同。我想我将问题识别为一个变量被解释为字符串而不是变量 这是我的密码:Python中的字符串和变量问题,python,list,global-variables,Python,List,Global Variables,我花了几个小时试图解决一个问题,但没法再进一步了。这是因为缺乏理解为什么一些变量的行为方式与我预期的不同。我想我将问题识别为一个变量被解释为字符串而不是变量 这是我的密码: # Global variables. fillers = ["__1__", "__2__", "__3__", "__4__"] difficulties = ["easy", "medium", "hard"] easy_text = "The Three Little Pigs is a fable about th
# 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_文本和easy_答案的显示
# 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
因此,我在这里根据所选的难度陈述了外语课文和外语答案。在我的例子中,它们变成了简单的文本和简单的答案
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_答案,我会得到简单的答案,但我想打印列表。所以为了保持它的动态性,我不能只打印简单的答案,它需要了解自己,我想打印简单的答案列表
随着我们在代码中的进一步深入,原因变得越来越清楚:
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)
我试图打印出FLU答案的列表元素1,以检查该元素是否与用户输入相同。但它确实给了我第二个简单答案。所以我想我确实声明了它应该是对列表的引用,而不是定义的变量
也不是说我希望代码的底线是从fl_文本中得到easy_文本,但它只是不执行它所写的fl_文本。如果我手动输入easy_文本,那么至少它会被执行。程序本身的功能还没有发展到我满意的程度
我希望有人能理解我的问题,帮助我理解我在这里忘记了什么基本原则。您的函数差异验证返回的是字符串而不是变量。因此,在打印时,应该打印字符串,而不是列表。要从匹配的变量名中获取列表变量,可以使用如下字典:
# 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]
嘿,谢谢你抽出时间。要访问列表,这很好。但是我怎样才能得到第一个列表元素呢?我尝试打印列表[fl_答案[0]]什么列表?easy_text=三只小猪是一个关于三只用不同材料建造三只小猪的寓言。一个大坏蛋uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。这个?在这种情况下,如果此答案解决了您的问题,则应打印列表[f1_text],将问题标记为已解决,以便其他人知道此问题有解决方案。否,请打印fl_答案列表。这个问题还没有解决,但我马上就要解决了。当然,我会在得到最终答案后尽快更新。感谢实际列表[f1\U答案]是列表。因此,要从列表中获取第i个元素,必须编写列表[f1_answers][i]。因此,对于第一个元素,它将是列表[f1_answers][0]