|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这样写为什么不能判断出闰年啊- tempt = input("please input:")
- if tempt.isdigit() == 0:
- print("wrong type")
- else:
- year = int(tempt)
- a = year/4
- b = year/100
- c = year/400
- if isinstance(c,int):
- print("闰年")
- else:
- if isinstance(a,int) and isinstance(b,float):
- print("闰年")
- else:
- print("不是闰年")
复制代码
本帖最后由 Twilight6 于 2020-7-25 21:54 编辑
因为 Python 中除法返回的都是 浮点型,导致你的 isinstance 判断是否为 int 整型时永远返回的都是 False
改成这样,用 int 后的值和原来除数的值进行比较,就能知道是否被整除 因为 1.0 是等于 1 的
- tempt = input("please input:")
- if tempt.isdigit() == 0:
- print("wrong type")
- else:
- year = int(tempt)
- a = year/4
- b = year/100
- c = year/400
- if c == int(c):
- print("闰年")
- else:
- if a == int(a) and b != int(b):
- print("闰年")
- else:
- print("不是闰年")
复制代码
或者用求余运算符,来判断是否被整除:
- tempt = input("please input:")
- if tempt.isdigit() == 0:
- print("wrong type")
- else:
- year = int(tempt)
- a = year%4
- b = year%100
- c = year%400
- if not c:
- print("闰年")
- else:
- if not a and b:
- print("闰年")
- else:
- print("不是闰年")
复制代码
|
|