gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:21:47 24 4
gpt4 key购买 nike

我正在使用 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__'

最佳答案

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(即什么都没有),这当然不会包含任何信息。

关于python - “NoneType”对象在 24 小时自学 Python 的第 9 章中没有属性 '__getitem__',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20767119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com