005讲 - 代码实战-找茬
本帖最后由 阿黎黎 于 2017-7-6 09:47 编辑Q1:以下代码有两处bug,找到并给出改进方法:
temp = input('请输入一个年份:')
while not temp.isdigit():
temp = input("抱歉,您的输入有误,请输入一个整数:")
year = int(temp)
if isinstance(year/400, int):
print(year + ' 是闰年!')
else:
if isinstance(year/4, int):
if isinstance(year/100, int):
print(year + ' 不是闰年!')
else:
print(year + ' 是闰年!')
else:
print(year + ' 不是闰年!')
Q2:“while not temp.isdigit(): ”语句为什么不用排除负数的影响呢?
**** Hidden Message *****
没有答出来的小伙伴们如果不介意的话,评个分再走可好{:9_221:} temp = input('请输入一个年份:')
while not temp.isdigit():
temp = input("抱歉,您的输入有误,请输入一个整数:")
year = int(temp)
if isinstance(year/400, int):
print(year + ' 是闰年!')
else:
if isinstance(year/4, int):
if isinstance(year/100, int):
print(str(year )+ ' 不是闰年!')
else:
print(str(year) + ' 是闰年!')
else:
print(str(year) + ' 不是闰年!')
Q1:只知道整型和字符串不能+{:10_266:}
print(year + ' 是闰年!')====>print(year,' 是闰年!') PwPython 发表于 2017-7-6 11:04
temp = input('请输入一个年份:')
while not temp.isdigit():
temp = input("抱歉,您的输入有误, ...
谢谢提供了拼接的新思路{:5_92:}
可以跑一下程序试一下哦,这个程序还是不对的{:5_100:}
Seven007 发表于 2017-7-6 11:17
Q1:只知道整型和字符串不能+
print(year + ' 是闰年!')====>print(year,' 是闰年!')
引号里的空格要去掉哦,逗号自占一个空格符{:5_92:} int与字符串不能拼接 为什么要/400呢,这是什么意思 李昊达 发表于 2017-7-7 21:14
为什么要/400呢,这是什么意思
isinstance(year / 400 , int)意思是当所输入的年份除以400为整数的时候,返回True,但这里“/”是浮点除法,所以永远为False。闰年的定义是能被4整除,但是不能被100整除。或者能被400整除。不好意,这几天有事情耽搁了{:5_100:} temp=input('请输入日期:')
while not temp.isdigit():
temp=input('请输入数字:')
year=int(temp)
if isinstance(year/4,int):
print(str(year)+'瑞年')
else:
print(str(year)+'不是瑞年')
页:
[1]