0
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()

好吧,所以唯一对我不起作用的是视图功能。添加事件后,然后我尝试查看它,我得到一个不可散列的类型:“列表”错误。为什么会这样,我该如何解决?谢谢

4

1 回答 1

1

您正在分配x = dictionary.keys(),这意味着 x 是一个列表(包含字典中的所有键)。然后你做if x in dictionary,它会询问键x是否是字典中的键。但是列表是可变的,所以它不允许作为键。

你真的是想说if theHour in dictionary吗?

于 2013-10-22T02:46:03.717 回答