Python 用字典调用函数的意义

Python 用字典调用函数的意义,python,list,function,dictionary,return,Python,List,Function,Dictionary,Return,我对函数和字典的使用感到非常困惑。在我用来学习python的书中有一个例子,但它并没有解释作者为什么这样写。这就是为什么我希望有人能向我解释为什么剧本是用这种形式写的。以下是脚本: def get_specials(): monday = {'B': 'Horseradish omelet. Note: better than it sounds', \ 'L': 'Momma\'s curry. Note: can be made spicy.',\ 'D': 'Bee

我对函数和字典的使用感到非常困惑。在我用来学习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': '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"'}
    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': '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"'}
    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}
    return specials

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/Sb): ")
        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()

我的怀疑主要在def主部分。有人能给我解释一下这整件事是怎么叫的吗?提前感谢

您可能应该更改get_day函数,以便提示符为每天提示映射到提示符的正确代码,例如,ST而不是Sb:

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)
然后,更改get_specials的返回值以匹配:

specials = {'M': monday, 'T': tuesday, 'W': wednesday, 'R': thursday, 'F': friday, 'ST': saturday, 'SN': sunday}
return specials
现在解释一下:

找一本字典的特辑

特价是每天的字典星期一,星期二。。。每一天都是一个有时间键的dict['B','L','D']

在主过程中,提示用户输入日期和时间。然后,主过程返回与日期对应的项目作为特殊项,并从中返回与时间键关联的值

在主程序中,此行返回特殊商品

如果我们回忆起上面的解释,这将返回一个字典字典:

特价是每天的字典星期一,星期二。。。和 每一天都是一个有时间键的dict['B','L','D']

这一行然后返回上面返回的第二级特价,因此,它返回特定日期的B/L/D字典

由于这也是一个由B/L/D时间键入的字典,因此我们可以检索特定时间的特殊信息:


基于非音速反斜杠,我建议你读一本不同的书。你可能会对完全不必要的反斜杠感兴趣,整个程序看起来像是从其他语言转换过来的,或者很糟糕。不幸的是,世界上有太多糟糕的教程。这甚至无法正常工作,因为get_day返回大写字符串,但这用于访问大小写混合键的词典。谢谢大家的建议!!learnpythonthehardway.org/book是一个很好的建议。我将很快从中学习python!!!非常感谢您的解释和帮助。我还有一个问题。为什么特别=特价[日期]和打印特价[时间]?我真的不明白这部分。为什么我不能把special=specials[day]和special=specials[time]?是的,如果名称==。。。指定调用模块时要运行的内容感谢您的回复。我的意思是他们之间有什么区别?你能给我解释一下这两个表达的意思吗?两者的区别是什么?这里只有一个表达,这些表达是什么意思?1特价=特价[日期]2打印特价[时间]打印特价[时间]和特价=特价[时间]有什么区别?为什么我可以写特价=特价[日期]而不是特价=特价[时间]。
specials = get_specials()
special = specials[day]
print_special(special[time])