I am trying to solve a problem for a couple of hours but can't get any further. Its regarding a lack of understanding why some variables are not behaving the way I expect them to. I think I identified the problem as a variable being interpreted as a string instead of a variable.
Heres my code:
# 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"]
So in this particular situation my problem revolves around the display of the variables easy_text and 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
so here I state the fl_text and fl_answers according to the difficulty chosen. In my example they become easy_text and 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
This now the point where I can't understand what is wrong in my head. If I print now fl_answers I get "easy_answers" but I want to get the list printed. So in order to keep it dynamic I can't just print "easy_answers" it needs to understand itself that I want to print the list of easy_answers.
as we go further in the code it becomes clear why:
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)
I try to print out the list element no 1 of fl_answers to check if this element is the same as the user input. But it does just give me the second letter of "easy_answers". So I figured out somehow I did declare it should be a reference to the list and not the variable defined.
Not also that the bottom line of the code I expect to get easy_text out of fl_text but it just doesn't execute it as long as its written fl_text. If I manually enter easy_text then at least its executed. The function of the program itself is not developed to my satisfaction yet.
I hope someone can understand my issue and help me understand what fundamental principle I forget here.