我正在用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__'发布于 2013-12-24 21:58:48
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 (即不包含任何信息),这当然不包含任何信息。
发布于 2013-12-24 21:59:48
您的问题是,get_specials()没有返回语句。因为它没有返回任何东西,所以行
specials = get_specials()将specials赋值为None,因此specials[day]试图在None中查找索引,该索引由于您看到的错误而失败。
https://stackoverflow.com/questions/20767119
复制相似问题