0

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.

1 Answer 1

1

Your function diff_validation() is returning a string not a variable. So when you are printing you should get the string printed not the list. To get the list variable from the matching variable name you can use dictionary like this:

# 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}

Then to retrieve:

fl_text, fl_answers = diff_validation()
print lists[f1_answers]
Sign up to request clarification or add additional context in comments.

4 Comments

hey thank you for your time. To access the list this works just fine. But how would i then be able to get the first list element? i tried print lists[fl_answers[0]]
what list? 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__." this one? In that case it should be print lists[f1_text] If this answer solves your problem mark the problem as solved so that others can know that this question has a solution.
no the list of fl_answers. it isnt solved yet but im close to getting there. of course i will update this as soon as i have the final answer. Thanks
Actually lists[f1_answers] is the list. So to get i'th element from the list you have to write lists[f1_answers][i]. So for first element it will be lists[f1_answers][0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.