【发布时间】:2016-09-27 23:09:35
【问题描述】:
我不确定我在这里做错了什么,有人知道吗?我不断收到一条错误消息,提示“元组索引超出范围”。我正在学习学校的教程,我似乎做的一切都是正确的,但是我一直收到这个错误。任何帮助,将不胜感激!非常感谢。
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()
【问题讨论】:
-
错误发生在哪里?你提供什么作为输入?
-
什么是storyDict = dict()?不是 storyDict = {}?
-
Scott,错误发生在 withSubstitutions = animalFormat.format(**dictionary)。我正在尝试使用用户输入的动物、小动物和武器来打印故事。如果这就是你要问的。
-
Omid,storyDict = dict() 是我被教导制作字典的方式。对吗?
-
@Sammy 没有。使用 dicts = {} 创建一个新的字典。此外,与其让多行分配值,不如像这样创建和初始化字典?
def get_dict(): return {'animal': animal, 'smallAnimal': smallAnimal, 'weapon', weapon}。更少打字 = 更好。