鱼C论坛

 找回密码
 立即注册
查看: 3260|回复: 4

第005讲:闲聊之Python的数据类型 闰年问题

[复制链接]
发表于 2015-9-27 15:14:26 | 显示全部楼层 |阅读模式
5鱼币
大家好

请问,我写的这段代码,为什么无论输入什么数字,出现的都是“不是闰年”呢,请大家指导下我哪块出错了,谢谢!

判断一个年份是否是闰年?
以下为我的代码~

  1. print ('请输入一个年份: ', end='')
  2. temp = input()

  3. if temp.isdigit():
  4.     year = int(temp)
  5.     con1 = year/4
  6.     con2 = year/100
  7.     con3 = year/400
  8.     if (isinstance(con1,int) and isinstance(con2,float)) or isinstance(con3,int):
  9.         print('是闰年')
  10.     else:
  11.         print('不是闰年')
  12. else:
  13.     print('输入的不是数字,系统结束')
复制代码

最佳答案

查看完整内容

你用的是Python3x吧,python3中16/4=4.0,type(16/4)-是,也即是浮点数,不是int, 因而isinstance(con1,int) == False,isinstance(con2,float)==True,两个逻辑and运算后为False,即 (isinstance(con1,int) and isinstance(con2,float)) == False,第三个isinstance(con3,int)也是一样等于Fale 那么再逻辑运算or后(两边都是Fale),当然永远False了,所以都会print('不是闰年')!! python2x中9/10=0,也就是向下取整,python3中 ...
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-9-27 15:14:27 | 显示全部楼层
你用的是Python3x吧,python3中16/4=4.0,type(16/4)-是<class 'float'>,也即是浮点数,不是int,
因而isinstance(con1,int) == False,isinstance(con2,float)==True,两个逻辑and运算后为False,即
(isinstance(con1,int) and isinstance(con2,float)) == False,第三个isinstance(con3,int)也是一样等于Fale
那么再逻辑运算or后(两边都是Fale),当然永远False了,所以都会print('不是闰年')!!
python2x中9/10=0,也就是向下取整,python3中都是4/2=2.0结果为浮点数的!

评分

参与人数 1荣誉 +3 鱼币 +5 收起 理由
~风介~ + 3 + 5 感谢楼主无私奉献!

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-9-27 20:41:09 | 显示全部楼层
夕阳游龙 发表于 2015-9-27 15:14
你用的是Python3x吧,python3中16/4=4.0,type(16/4)-是,也即是浮点数,不是int,
因而isinstance(con1,in ...

感谢帮助!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-9-29 10:46:29 | 显示全部楼层
版本原因真的比较苦恼
何况,计算机永远是对的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-10-16 10:31:46 | 显示全部楼层
学习了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-17 19:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表