“元组索引超出范围”错误?

2024-10-01 13:38:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道我做错了什么,有人知道吗?我一直收到一个错误,说“元组索引超出范围”。我正在跟随学校的教程,我似乎做的每件事都是正确的,但是我一直得到这个错误。任何帮助都将不胜感激!非常感谢你。在

animal = input("Enter any LARGE animal: ")
smallAnimal = input("Enter any SMALL animal: ")
weapon = input("Enter a sharp weapon: ")

def createDictionary():

    storyDict = dict()
    storyDict['animal'] = animal
    storyDict['smallAnimal'] = smallAnimal
    storyDict['weapon'] = weapon

return storyDict

def main():

    dictionary = createDictionary()

    animalFormat = """Once upon a time, there was a very, very large {animal}. This {animal} was the meanest, baddest, most gruesome {animal} there     was. And one day, a wild {1} had
    stepped on the {animal}'s foot. At that moment, the {1} knew it had messed up. This made the {animal} angry, so he took a {weapon} and STABBED the 
    {smallAnimal}with it! The {smallAnimal} squirmed and fought to get out, but it was no match for the {animal} with a {weapon}.

    The End."""

    withSubstitutions = animalFormat.format(**dictionary)
    print(withSubstitutions)


main()

Tags: theinputdictionarymaindef错误anyit
3条回答

在animalFormat中,替换:

{1}

有:

^{pr2}$

这个改变必须在两个地方进行。在

由于使用关键字为format提供参数,因此1没有可引用的内容。在

更简单的例子

请注意这一点:

>>> d = {'a':1, 'b':2}
>>> 'Twice {a} is {b}'.format(**d)
'Twice 1 is 2'

但这行不通:

>>> 'Twice {1} is {b}'.format(**d)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range

如果为format提供关键字参数,则1没有可引用的内容。在

可以同时提供位置参数和关键字参数。例如:

>>> 'Twice {1} is {b}'.format('Arg0', 'ArgOne', **d)
'Twice ArgOne is 2'

完成工作代码

animal = input("Enter any LARGE animal: ")
smallAnimal = input("Enter any SMALL animal: ")
weapon = input("Enter a sharp weapon: ")

def createDictionary():

    storyDict = dict()
    storyDict['animal'] = animal
    storyDict['smallAnimal'] = smallAnimal
    storyDict['weapon'] = weapon

    return storyDict

def main():

    dictionary = createDictionary()

    animalFormat = """Once upon a time, there was a very, very large {animal}. This {animal} was the meanest, baddest, most gruesome {animal} there     was. And one day, a wild {smallAnimal} had
    stepped on the {animal}'s foot. At that moment, the {smallAnimal} knew it had messed up. This made the {animal} angry, so he took a {weapon} and STABBED the_
    {smallAnimal}with it! The {smallAnimal} squirmed and fought to get out, but it was no match for the {animal} with a {weapon}.

    The End."""

    withSubstitutions = animalFormat.format(**dictionary)
    print(withSubstitutions)


main()

样本运行:

Enter any LARGE animal: Lion
Enter any SMALL animal: Mouse
Enter a sharp weapon: knife
Once upon a time, there was a very, very large Lion. This Lion was the meanest, baddest, most gruesome Lion there     was. And one day, a wild Mouse had
    stepped on the Lion's foot. At that moment, the Mouse knew it had messed up. This made the Lion angry, so he took a knife and STABBED the 
    Mousewith it! The Mouse squirmed and fought to get out, but it was no match for the Lion with a knife.

    The End.

此错误是因为您有命名替换(例如.. very large {animal} ..)以及位置替换(例如At that moment, the {1} knew ..)。在

考虑传递类似于:

animalFormat.format("Replacement for {0}", "Replacement for {1}", **dictionary)

在上述情况下,子基的位置部分将为:

^{pr2}$

您正在按关键字和有序数据混合格式化字符串。在

MCVE公司:

s = "{0}{k}".format(k='something')

例外情况:

^{pr2}$

由于模板包含有序参数的占位符({1}),因此需要将它们传递给函数:

animalFormat = """Once upon a time, there was a very, very large {animal}. This {animal} was the meanest, baddest, most gruesome {animal} there     was. And one day, a wild {1} had
stepped on the {animal}'s foot. At that moment, the {1} knew it had messed up. This made the {animal} angry, so he took a {weapon} and STABBED the 
{smallAnimal}with it! The {smallAnimal} squirmed and fought to get out, but it was no match for the {animal} with a {weapon}.

The End."""

d = {'animal': 'X', 'smallAnimal': 'Y', 'weapon': 'Z'}
a = ['A', 'B']  # placeholder
animalFormat.format(*a, **d)  # works fine

相关问题 更多 >