鱼C论坛

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

[已解决]小甲鱼作业5 闰年问题

[复制链接]
发表于 2020-2-11 17:57:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
temp = input('请输入一个年份:')
while not temp.isdigit():
    print('抱歉,输入有误请重新输入:')
    temp = input()
year = int(temp)
a = year/4
b = year/100
c = year/400
if a.isdigit() and not b.isdigit():
    print(year + '是闰年')
else:
    if c.isdigit():
        print(year + '是闰年')
    else:
        print(year + '不是闰年')
会报错
line 9, in <module>
    if a.isdigit() and not b.isdigit():
AttributeError: 'float' object has no attribute 'isdigit'
请问如何解决

最佳答案
2020-2-11 18:43:54
isdigit() 使用有误。不建议你用这种方法。可以使用 % 运算符取余:

  1. temp = input('请输入一个年份:')
  2. while not temp.isdigit():
  3.     print('抱歉,输入有误请重新输入:')
  4.     temp = input()
  5. year = int(temp)
  6. if year % 4 == 0 and year % 100 != 0:
  7.     print(year, "是闰年!")
  8. else:
  9.     if year % 400 == 0:
  10.         print(year, "是闰年!")
  11.     else:
  12.         print(year, "不是闰年!")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-11 18:03:24 | 显示全部楼层
  1. if a.isdigit() and not b.isdigit():
  2.     print(year + '是闰年')
  3. else:
  4.     if c.isdigit():
  5.         print(year + '是闰年')
  6.     else:
  7.         print(year + '不是闰年')
复制代码



把这一段的isdigit去掉
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-11 18:43:54 | 显示全部楼层    本楼为最佳答案   
isdigit() 使用有误。不建议你用这种方法。可以使用 % 运算符取余:

  1. temp = input('请输入一个年份:')
  2. while not temp.isdigit():
  3.     print('抱歉,输入有误请重新输入:')
  4.     temp = input()
  5. year = int(temp)
  6. if year % 4 == 0 and year % 100 != 0:
  7.     print(year, "是闰年!")
  8. else:
  9.     if year % 400 == 0:
  10.         print(year, "是闰年!")
  11.     else:
  12.         print(year, "不是闰年!")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-2-12 10:20:46 | 显示全部楼层
zltzlt 发表于 2020-2-11 18:43
isdigit() 使用有误。不建议你用这种方法。可以使用 % 运算符取余:

谢谢!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-12 10:21:22 | 显示全部楼层
SHRS23 发表于 2020-2-11 18:03
把这一段的isdigit去掉

谢谢 我试试康!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-3 12:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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