合并多个if ...打印语句

时间:2019-09-08 23:29:54

标签: 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")

4 个答案:

答案 0 :(得分:2)

一个想法是使用字典:

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)

答案 1 :(得分:1)

当if仅用于映射1x1值时,我喜欢使用字典

weekmap = dict(
  sunday=sunday,
  monday=monWed,
  wednesday=monWed,
  tuesday=tuesday,
  thursday=thursday,
  friday=friday,
  saturday=saturday)

print(weekmap.get(dayOfWeek, "else part"))

答案 2 :(得分:1)

使用dict

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!'}

创建函数:

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!

答案 3 :(得分:-1)

希望这会有所帮助!

 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":
     result = sunday

 elif dayOfWeek == "monday":
     result = monWed

 elif dayOfWeek == "wednesday":
     result = monWed

 elif dayOfWeek == "tuesday":
     result = tuesday

 elif dayOfWeek == "thursday":
     result = thursday

 elif dayOfWeek == "friday":
     result = friday

 elif dayOfWeek == "saturday":
     result = saturday

 else:
     result = "Check your spelling and try again"

 print(result)

为避免使用print语句,您需要将值存储在一个临时变量中。