首页 > 其他分享 >4.14

4.14

时间:2024-06-19 17:53:43浏览次数:13  
标签:set 4.14 self month year input day

时长:三个小时

问题:

8-4 【Python0017】设计异常处理类Cexception,并基于异常处理类设计并实现日期类Date 分数 10 作者 doublebest 单位 石家庄铁道大学

【题目描述】定义一个异常类Cexception解决日期类实现中的自定义异常处理。设计的日期类应包含以下内容:

① 有三个成员数据:年、月、日;

② 有设置日期的成员函数;

③ 有用格式"月/日/年"输出日期的成员函数;

④ 要求在日期设置及有参构造函数中添加异常处理。

程序中定义各种日期对象并测试。
【注意事项】闰年的 2 月的天数为 29天,其它年份 2 月28 天;闰年是指:年份能被 4且不能被 100 整除,或者年份能被 400 整除。

注意日期间的关联。
【练习要求】请给出源代码程序和运行测试结果,源代码程序要求添加必要的注释。

代码量:

class CException(Exception):
"""自定义日期相关的异常类"""
def __init__(self, message):
super().__init__(message)

class Date:
def __init__(self, year_input=None, month_input=None, day_input=None):
if year_input is None or month_input is None or day_input is None:
self.prompt_and_set_date()
else:
try:
self.set_year(year_input)
self.set_month(month_input)
self.set_day(day_input)
except CException as e:
print(f"错误:{e}")

def prompt_and_set_date(self):
while True:
try:
year = int(input("请输入年份: "))
month = int(input("请输入月份: "))
day = int(input("请输入日期: "))
self.set_year(year)
self.set_month(month)
self.set_day(day)
break # 成功设置日期后退出循环
except ValueError:
print("输入无效,请确保输入的是整数。")
except CException as e:
print(f"错误:{e}")

def set_year(self, year):
if year < 1:
raise CException("年份不是正整数。")
self.year = year

def set_month(self, month):
if not (1 <= month <= 12):
raise CException("月份错误。")
self.month = month

def set_day(self, day):
if not (1 <= day <= self.get_max_day()):
raise CException("日期范围错误")
self.day = day

def get_max_day(self):
if self.month == 2:
if self.is_leap_year():
return 29
else:
return 28
elif self.month in [4, 6, 9, 11]:
return 30
else:
return 31

def is_leap_year(self):
return (self.year % 4 == 0 and self.year % 100 != 0) or (self.year % 400 == 0)

def display(self):
return f"{self.month}/{self.day}/{self.year}"

# 程序测试 - 通过控制台输入
print("请输入日期(或输入错误的日期以测试异常处理):")
date = Date()
print(f"设置的日期为: {date.display()}")

标签:set,4.14,self,month,year,input,day
From: https://www.cnblogs.com/szm123/p/18256911

相关文章

  • 关于报错:npm下载时出现node_modules\.pnpm\[email protected]\node_modules\node-s
    关于npminstall出现Failedin911msatF:\Sert-Vue-master\vue\node_modules\.pnpm\[email protected]\node_modules\node-sass ELIFECYCLE Commandfailedwithexitcode1. 最开始从GitHub拉了一个项目,然后npmi的时候出现了报错npmERR!gypERR!stackError:`......
  • 上周热点回顾(4.8-4.14)
    热点随笔:· 园子周边第3季—设计初稿预览:2024夏天穿上博客园T恤showyourcode (博客园团队)· 园子开店记:被智能的淘宝处罚,说是“预防性的违规” (博客园团队)· 拥抱开源更省钱「GitHub热点速览」 (削微寒)· C#使用PaddleOCR进行图片文字识别✨ (mingupupup)· .NE......
  • 2024.4.14
    2024.4.14【言念君子,温其如玉。】Sunday三月初六<theme=oi-"search"><BGM=Realms>模拟赛!!!P1274魔术数字游戏就摁模搜索就行,我是实在打不下去了。考试时200行白打了P1275魔板//2024.4.14//bywhite_ice#include<bits/stdc++.h>usingnamespacestd;#defin......
  • q3-瞎汤姆养-2024.4.14
    第一批买的黑水虻孵化卵的时候没管理好,人为的搞死了,然后又买了一批,这批感觉活了点,这批是4月6日买的。不过看在鸡粪里不是特别活跃,是不是温度太高或者太低了,这个我不太知道。主要还是后期管理需要弄家里一直不给我地方搞,这个还需要空间才能玩得转,还要防止它跑出去,成虫是会飞的。......
  • CleanMyMac X 4.14.6 Mac系统清理工具 核心功能讲解
    随着时间的推移,我们的Mac电脑往往会变得越来越慢,存储空间变得越来越紧张,这时候一个优秀的清理工具就显得尤为重要。CleanMyMacX 作为一款备受好评的Mac清理工具,它能够为你的Mac带来全方位的清理和优化。在本文中,我们将深入评测CleanMyMacX的功能,看看它是否能够成为你Mac的得......
  • 聊聊CWE 4.14 与 ISA/IEC 62443中,如何保障工业软件的安全性
    本文分享自华为云社区《CWE4.14与ISA/IEC62443》,作者:Uncle_Tom。1.序言随着5G的应用,物联的网发展,越来越多的自动化控制系统、云服务在工业控制系统被广泛使用。为了实现生产自动化,很多企业都引入了由PLC(可编程逻辑控制器)控制的自动化生产设备和相关的自动化生产系统。用......
  • 安装install的项目的报错sass npm ERR! [email protected] postinstall: `node scripts
    npmERR!codeELIFECYCLEnpmERR!errno1npmERR!node-sass@4.14.1postinstall:`nodescripts/build.js`npmERR!Exitstatus1npmERR!npmERR!Failedatthenode-sass@4.14.1postinstallscript.npmERR!Thisisprobablynotaproblemwithnpm.Thereis......
  • CleanMyMac X4.14.1中文版如何清理 Mac系统?
    CleanMyMacX4.14.1中文版如何清理Mac系统?Mac系统在使用过程中都会产生大量系统垃圾,如不需要的系统语言安装包,视频网站缓存文件,mac软件卸载残留的注册表等。随着时间推移,mac系统垃圾就会越来越多,电脑就开始变慢变卡。CleanMyMacX可以帮你快速清理mac系统垃圾。CleanMyMacX4.14......
  • npm install报错[email protected] postinstall: `node scripts/build.js`
    [email protected]: nodescripts/build.js解决方法:npmconfigsetsass_binary_site=https://npm.taobao.org/mirrors/node-sassnpminstall  ......
  • 4.14学习总结
    androidstdio中button的按下与松开实现图标转换 图片:首先在res目录下的drawable文件夹下创建select功能的.xml文件,然后下载两张图片drawable文件夹(图片名称开头不可以是数字),第二步,在.xml文件中添加如下两行代码<itemandroid:drawable="@drawable/图片名称"android:stat......