'NoneType'对象在24小时内自学Python教学的第9章中没有属性'__getitem__'

时间:2013-12-24 21:52:10

标签: python

我正在使用2.7.6完成本书,我收到了一个错误。我逐行完成了这一点,以确保没有任何拼写错误。我(显然)对Python知之甚少,甚至不知道从哪里开始。我很感激我能得到的任何帮助。

以下是代码:

def get_specials():
        monday = {'B': 'Horseradish omelet. Note: Better than it sounds.',
                  'L': 'Momma\'s Curry.  Note: Can be made spicy.',
                  'D': 'Beef brisket.  Note:Comes with au jus. That\'s pronounced "Oh jhoo", not "Ow Juice."'}

        tuesday = {'B': 'Sausage gravy over biscuits. Note: Toast can be subbed.',
                   'L': 'Grilled cheese and tomato soup. Note: We have vegan cheese.',
                   'D': 'Meatloaf. Note: Comes with catsup on the top. Not optional.'}

        wednesday = {'B': 'Horseradish omelet. Note: Better than it sounds.',
                  'L': 'Momma\'s Curry.  Note: Can be made spicy.',
                  'D': 'Beef brisket.  Note:Comes with au jus. That\'s pronounced "Oh jhoo", not "Ow Juice."'}

        thursday = {'B': 'Sausage gravy over biscuits. Note: Toast can be subbed.',
                   'L': 'Grilled cheese and tomato soup. Note: We have vegan cheese.',
                   'D': 'Meatloaf. Note: Comes with catsup on the top. Not optional.'}

        friday = {'B': 'Horseradish omelet. Note: Better than it sounds.',
                  'L': 'Momma\'s Curry.  Note: Can be made spicy.',
                  'D': 'Beef brisket.  Note:Comes with au jus. That\'s pronounced "Oh jhoo", not "Ow Juice."'}

        saturday = {'B': 'Sausage gravy over biscuits. Note: Toast can be subbed.',
                   'L': 'Grilled cheese and tomato soup. Note: We have vegan cheese.',
                   'D': 'Meatloaf. Note: Comes with catsup on the top. Not optional.'}

        sunday = {'B': 'Horseradish omelet. Note: Better than it sounds.',
                  'L': 'Momma\'s Curry.  Note: Can be made spicy.',
                  'D': 'Beef brisket.  Note:Comes with au jus. That\'s pronounced "Oh jhoo", not "Ow Juice."'}

        specials = {'M': monday,
                    'T': tuesday,
                    'W': wednesday,
                    'R': thursday,
                    'F': friday,
                    'St': saturday,
                    'Sn': sunday}

def print_special(special):
        print "The special is:"
        print special
        print "*"*15

def get_day():
        while True:
                day = raw_input("Day (M/T/W/R/F/St/Sn): ")
                if day.upper() in ['M', 'T', 'W', 'R', 'F', 'ST', 'SN']:
                        return day.upper()
                else:
                        print "I'm sorry, but {} isn't valid.".format(day)

def get_time():
        while True:
                time = raw_input("Time (B/L/D): ")
                if time.upper() in ['B', 'L', 'D']:
                        return time.upper()
                else:
                        print "I'm sorry, but {} isn't a valid time.".format(time)

def main():
        specials = get_specials()
        print "This script will tell you the specials for any day of the week, and any time."
        while True:
                day = get_day()
                special = specials[day]
                time = get_time()
                print_special(special[time])
                another = raw_input("Do you want to check another day and time? (Y/N")
                if another.lower() == 'n':
                        break

if __name__ == '__main__':
        main()

错误:

Traceback (most recent call last):
  File "C:/Users/User/Desktop/Python/9.py", line 71, in <module>
    main()
  File "C:/Users/User/Desktop/Python/9.py", line 64, in main
    special = specials[day]
TypeError: 'NoneType' object has no attribute '__getitem__'

2 个答案:

答案 0 :(得分:4)

get_specials函数最后应返回specials字典:

def get_specials():
    # …

    specials = {'M': monday,
                'T': tuesday,
                'W': wednesday,
                'R': thursday,
                'F': friday,
                'St': saturday,
                'Sn': sunday}
    return specials

这样,当你specials = get_specials()时,你实际上得到了该词典的内容。如果不这样做,则调用get_specials()将返回None(即没有),当然不会包含任何信息。

答案 1 :(得分:2)

您的问题是get_specials()没有返回声明。由于它没有返回任何东西,行

specials = get_specials()

specials指定为None,以便specials[day]尝试查找None中的索引,该索引因您看到的错误而失败。