exceptions.TypeError不可用类型列表。我该如何解决?

时间:2013-10-22 01:48:16

标签: python-2.7

import time

dictionary = {}

def add():
    x = int(raw_input("Please enter the hour of your event in 24 hour time"))  
    y = raw_input("Please enter the name of your activity")  
    dictionary[x] = y  
    print (dictionary[x] + " was added succesfully")  
    main()  

def view():
    theTime = time.localtime()  
    print "the hour is: ", theTime.tm_hour  
    theHour = theTime.tm_hour  
    x = dictionary.keys()  
    if x in dictionary:  
        print "The current event is", x  
    print "Your full shcedule for today is: ", dictionary  
    main()

def remove():
    print dictionary  
    x = int(raw_input("which time slot would you like to clear?"))  
    z = dictionary.pop(x)  
    print z + " was removed"  
    main()  

table = {
    1 : add,
    2 : view,
    3 : remove
    }

def run(x):
    table[x]()

def main():
    x = int(raw_input("Please select and option! \n\n1. Add an event \
\n2. View an event \n3. Remove an event \n4. Exit"))  
    if x == 4:  
        exit()  
    run(x)  

main()

好吧,所以对我来说唯一不起作用的是视觉功能。在我添加一个事件后,然后我尝试查看它,我得到一个不可用的类型:" list"错误。为什么这样,我该如何解决?谢谢

1 个答案:

答案 0 :(得分:1)

您正在分配x = dictionary.keys(),这意味着x是一个列表(字典中所有键的列表)。然后执行if x in dictionary,询问密钥x是否是字典中的密钥。但是列表是可变的,因此不允许它成为密钥。

你真的想说if theHour in dictionary吗?