3 回答
TA贡献1828条经验 获得超13个赞
一个想法是使用字典:
calendar = {
'sunday': "Just talmud today",
'monday': "Talmud \nComputing Theory",
'tuesday': "Talmud Klali \nIntro To Programming \nHistory \nEnglish Composition II",
'wednesday': "Talmud \nComputing Theory",
'thursday': "Shiur \nIntro To Programming \nHistory \nEnglish Composition II",
'friday': "Lecture",
'saturday': "Get off the computer!"
}
print("Welcome to your calendar")
day = input("What day is it? ").lower()
result = calendar.get(day, "Check your spelling and try again")
print(result)
TA贡献1982条经验 获得超2个赞
使用dict:
Python 中的字典
Python中的基本数据类型
day_response = {'sunday': 'Just talmud today',
'monday': 'Talmud \nComputing Theory',
'tuesday': 'Talmud Klali \nIntro To Programming \nHistory \nEnglish Composition II',
'wednesday': 'Talmud \nComputing Theory',
'thursday': 'Shiur \nIntro To Programming \nHistory \nEnglish Composition II',
'friday': 'Lecture',
'saturday': 'Get off the computer and do something my family would disapprove of!'}
创建一个函数:
Python 函数教程
此schedule函数将继续要求输入,直到提供正确的值。
“结合所有这些 if 语句以使用一个打印函数而不是七个”
根据要求,此功能print在 7 天内有一个
注意,根据PEP8dayOfweek更改为:函数和变量名称day_of_week
Python“while”循环(无限迭代)
Python 异常:简介
Python KeyError 异常以及如何处理它们
def schedule():
print('Welcome to your calendar')
while True:
day_of_week = input("What day is it? ").lower()
try:
print(day_response[day_of_week])
break
except KeyError:
print('Check your spelling and try again')
schedule()
输出schedule():
Welcome to your calendar
What day is it? daf
Check your spelling and try again
What day is it? ad
Check your spelling and try again
What day is it? saturday
Get off the computer and do something my family would disapprove of!
TA贡献1836条经验 获得超13个赞
当 ifs 用于简单地映射 1x1 值时,我喜欢使用 dicts
weekmap = dict(
sunday=sunday,
monday=monWed,
wednesday=monWed,
tuesday=tuesday,
thursday=thursday,
friday=friday,
saturday=saturday)
print(weekmap.get(dayOfWeek, "else part"))
添加回答
举报