猿问

合并多个 if ... print 语句

我们应该制作一个日程安排程序,您可以在其中输入日期,程序会返回您的日程安排。每天都有自己的时间表(周一和周三共享相同)。我只参加了这门 Python 课程 2 周,所以我们只知道基础知识。我怎样才能把它归结为更少的代码?


我的教授告诉我将所有这些 if 语句结合起来使用一个打印函数而不是七个。我通过定义 if 语句之前的天数来缩短它,但我不知道如何将它归结为一个打印命令


 sunday = "Just talmud today"

 monWed = "Talmud \nComputing Theory"

 tuesday = "Talmud Klali \nIntro To Programming \nHistory \nEnglish Composition II"

 thursday = "Shiur \nIntro To Programming \nHistory \nEnglish Composition II"

 friday = "Lecture"

 saturday = "Get off the computer!"


 print("Welcome to your calendar")

 dayOfWeek = input("What day is it? ")

 dayOfWeek = dayOfWeek.lower()


if dayOfWeek == "sunday":

     print(sunday)


 elif dayOfWeek == "monday":

     print(monWed)


 elif dayOfWeek == "wednesday":

     print(monWed)


 elif dayOfWeek == "tuesday":

     print(tuesday)


 elif dayOfWeek == "thursday":

     print(thursday)


 elif dayOfWeek == "friday":

     print(friday)


 elif dayOfWeek == "saturday":

     print(saturday)


 else:

     print("Check your spelling and try again")


明月笑刀无情
浏览 138回答 3
3回答

慕田峪7331174

一个想法是使用字典: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)

临摹微笑

使用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_weekPython“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 calendarWhat day is it?  dafCheck your spelling and try againWhat day is it?  adCheck your spelling and try againWhat day is it?  saturdayGet off the computer and do something my family would disapprove of!

开心每一天1111

当 ifs 用于简单地映射 1x1 值时,我喜欢使用 dictsweekmap = dict(  sunday=sunday,  monday=monWed,  wednesday=monWed,  tuesday=tuesday,  thursday=thursday,  friday=friday,  saturday=saturday)print(weekmap.get(dayOfWeek, "else part"))
随时随地看视频慕课网APP

相关分类

Python
我要回答