Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 归还你需要的咒语_Python_Python 3.x - Fatal编程技术网

Python 归还你需要的咒语

Python 归还你需要的咒语,python,python-3.x,Python,Python 3.x,我必须从HP Codewars 2012返回spellbinder程序,我不知道我在做什么。这是我到目前为止的资料,请帮忙。我正在使用python 3.3.0。这里是链接 我不想这么做 你应该试着去调试和弄清楚这些东西 def fix(item): '''The function of the repaired by a speelbot abused Dictionary word -> word to be repaired a -> the p

我必须从HP Codewars 2012返回spellbinder程序,我不知道我在做什么。这是我到目前为止的资料,请帮忙。我正在使用python 3.3.0。这里是链接


我不想这么做

你应该试着去调试和弄清楚这些东西

def fix(item):
    '''The function of the repaired by a
    speelbot abused Dictionary

    word -> word to be repaired
    a -> the point-to-exchange
    b -> the correct letter

    '''
    try:
        word, a, b = item.split(" ")
    except ValueError:
        print "Invalid Input"

    return word.replace(a, b)

if __name__ == "__main__":
    for w in ["MUSTARD M C", "JUNK J TR", "MONSTER ON A"]:
        print w, "-> ", fix(w)

欢迎来到StackOverflow!从你的问题来看,我不知道这段代码应该做什么。目前,你将一个字符串拆分为空,并期望一个3的列表。在for循环的字符串列表中有不匹配的引号。您需要更具体地说明问题和目标。您已接近目标,首先需要在空格上拆分,而不是在“”上拆分,因此拆分(“”),确保字符串正确打开和关闭,'“在打开和关闭时不可互换。你在替换行上打错了字,对吗@Denmat运行它,看看你得到了什么。很接近,但你还是错过了最后一点。你犯的错误会告诉你出了什么问题。
def fix(item):
    '''The function of the repaired by a
    speelbot abused Dictionary

    word -> word to be repaired
    a -> the point-to-exchange
    b -> the correct letter

    '''
    try:
        word, a, b = item.split(" ")
    except ValueError:
        print "Invalid Input"

    return word.replace(a, b)

if __name__ == "__main__":
    for w in ["MUSTARD M C", "JUNK J TR", "MONSTER ON A"]:
        print w, "-> ", fix(w)