2

谁能告诉我为什么我会收到这个错误?我认为代码没有问题,当我替换**itemName,Start,End我仍然无法让它工作

print("To finish input enter nothing.")
Schedule = []
Finish = False
while not Finish:
    Name = input("What is the name of the show?: ")
    Start = input("What time does the show start?: ")
    End = input("What time does the show end?: ")
    Schedule.append({'Name':Name, 'Start':Start, 'End':End})
    print("{0:<10}  |  {1:<10}  -  {2:<10}".format(Name,Start,End))
    print("{Name:<10}  |  {Start:<10}  -  {End:<10}  ".format(**item))
    if len(Name) == 0 or len(Start) == 0 or len(End) == 0:
        Finish = True
4

3 回答 3

3

你永远不会创造item.

item = {'Name':Name, 'Start':Start, 'End':End}
Schedule.append(item)
于 2013-08-08T00:53:46.847 回答
0
print("{Name:<10}  |  {Start:<10}  -  {End:<10}  ".format(**item))

这段代码对我来说很好,除了你没有定义项目?

假设该项目是某种容器,例如 (Name, Start, End),您需要将此行更改为:

print("{:<10}  |  {:<10}  -  {:<10}  ".format(**item))

这样您就不会使用密钥,它可以按顺序填写字段。或者:

print("{Name:<10}  |  {Start:<10}  -  {End:<10}  ".format(Name=item[0],
                                                          Start=item[1],
                                                          End=item[2]))

如果您真的打算使用密钥。

基本上,当您没有为该键提供值时,您的键错误来自使用键 ({ Name :<10}) 请求值。

希望有帮助。

于 2013-08-07T21:09:06.237 回答
0

尝试这个:

print("To finish input enter nothing.")
Schedule = []
Finish = False
while not Finish:
    Name = raw_input("What is the name of the show?: ")
    Start = raw_input("What time does the show start?: ")
    End = raw_input("What time does the show end?: ")
    Schedule.append({'Name':Name, 'Start':Start, 'End':End})
    print("{0:<10}  |  {1:<10}  -  {2:<10}".format    (Name,Start,End))
    print("{Name:<10}  |  {Start:<10}  -  {End:<10}  ".format    (**item))
    if len(Name) == 0 or len(Start) == 0 or len(End) == 0:
        Finish = True
于 2013-08-06T17:48:09.860 回答