Python学习记录01 使用字典模拟Switch…Case

141次阅读
没有评论

共计 659 个字符,预计需要花费 2 分钟才能阅读完成。

今天学习Python,期间需要使用到Switch...Case,下意识的写了一下,发现竟然不支持,在网上搜索了一下找到了替代的办法使用字典来模拟Switch...Case

print('模拟switc')

def taskForSunday():
    print("今天休息")
def taskForRest():
    print("今天休息")
def taskForChinese():
    print("今天上语文课")
def taskForMath():
    print("今天上数学课")
def taskForEnglish():
    print("今天上英语课")
def taskForDefault():
    print("输入错误啦。。。。")

switchDic = {"Sunday":taskForRest,
            "Monday":taskForChinese,
            "Tuesday":taskForMath,
            "Wednesday":taskForEnglish,
            "Tursday":taskForEnglish,
            "Friday":taskForEnglish,
            "Saturday":taskForRest
}

day1 = "Monday"
switchDic.get(day1,taskForDefault)() #打印:今天上语文课
day2 = "Friday"
switchDic.get(day2,taskForDefault)()  #打印:今天上英语课
day3 = "天气不错哦"
switchDic.get(day3,taskForDefault)() #打印:输入错误啦。。。。

正文完
 0
Eric chan
版权声明:本站原创文章,由 Eric chan 于2020-03-18发表,共计659字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。