为了账号安全,请及时绑定邮箱和手机立即绑定

合并多个 if ... print 语句

合并多个 if ... print 语句

明月笑刀无情 2022-05-24 12:49:05
我们应该制作一个日程安排程序,您可以在其中输入日期,程序会返回您的日程安排。每天都有自己的时间表(周一和周三共享相同)。我只参加了这门 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")
查看完整描述

3 回答

?
慕田峪7331174

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)


查看完整回答
反对 回复 2022-05-24
?
临摹微笑

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!


查看完整回答
反对 回复 2022-05-24
?
开心每一天1111

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"))


查看完整回答
反对 回复 2022-05-24
  • 3 回答
  • 0 关注
  • 137 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信