【问题标题】:String and Variable issues in PythonPython 中的字符串和变量问题
【发布时间】:2018-04-17 09:52:16
【问题描述】:

我试图解决一个问题几个小时,但无法进一步解决。它关于缺乏理解为什么某些变量的行为不像我期望的那样。我想我将问题确定为被解释为字符串而不是变量的变量。

这是我的代码:

# 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”的第二个字母。所以我发现我确实声明它应该是对列表的引用,而不是定义的变量。

也不是我希望从 fl_text 中获取 easy_text 的代码的底线,但只要编写 fl_text,它就不会执行它。如果我手动输入easy_text,那么至少它会被执行。程序本身的功能还没有开发到我满意的程度。

我希望有人能理解我的问题并帮助我理解我在这里忘记了什么基本原则。

【问题讨论】:

    标签: python list global-variables


    【解决方案1】:

    您的函数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]
    

    【讨论】:

    • 嘿,谢谢您的宝贵时间。要访问列表,这工作得很好。但是我怎么能得到第一个列表元素呢?我试过print lists[fl_answers[0]]
    • 什么列表? 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__."这个?在这种情况下,它应该是 print lists[f1_text] 如果此答案解决了您的问题,请将问题标记为已解决,以便其他人可以知道此问题有解决方案。
    • 没有 fl_answers 列表。它还没有解决,但我快到那里了。当然,一旦我得到最终答案,我会立即更新。谢谢
    • 其实lists[f1_answers]就是列表。因此,要从列表中获取第 i 个元素,您必须编写 lists[f1_answers][i]。所以对于第一个元素,它将是 lists[f1_answers][0]
    猜你喜欢
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 2017-02-27
    • 1970-01-01
    相关资源
    最近更新 更多