首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“NoneType”对象在“24小时内自学Python”的第9章中没有属性'__getitem__‘

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

Stack Overflow用户
提问于 2013-12-24 21:52:09
回答 2查看 78关注 0票数 2

我正在用2.7.6来完成这本书,我得到了一个错误。我已经逐行检查过,以确保没有任何排字。我(显然)对Python还不太了解,甚至不知道从哪里开始。如果能帮上忙我会很感激的。

以下是代码:

代码语言:javascript
复制
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()

而错误是:

代码语言:javascript
复制
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__'
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-24 21:58:48

get_specials函数应该在结束时返回specials字典:

代码语言:javascript
复制
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 (即不包含任何信息),这当然不包含任何信息。

票数 4
EN

Stack Overflow用户

发布于 2013-12-24 21:59:48

您的问题是,get_specials()没有返回语句。因为它没有返回任何东西,所以行

代码语言:javascript
复制
specials = get_specials()

specials赋值为None,因此specials[day]试图在None中查找索引,该索引由于您看到的错误而失败。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20767119

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档