共计 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)() #打印:输入错误啦。。。。
正文完