有一个条件我不得不使用return而不是print,因为如果我使用print,if条件将不会继续(如果我错了就纠正我),现在我需要查看结果(在某些网站中正如代码一样,他们如何将事物放在一起来检查解决方案是否正确)
def centuryFromYear(year):
"""Calculates the century for a given year:
Example: year 1 = 1, year 100 = 1, year 101 = 2 year 1954 = 19 etc."""
if len(str(year)) == 1 or 2:
return 1
if len(str(year)) == 3:
if year[1:3] == 0:
return year[0]
else:
return year[0] + 1
if len(str(year)) == 4:
if year[1:4] == 0:
return year[0] + 0
else:
return year[0:2] + 1
答案 0 :(得分:0)
这不是一个真正的逻辑问题,它更像是一个数学问题 - 不要将你的int转换为字符串 - 而是计算世纪:
<button type="button" class="dd" data="dropdownMenuButton2">click me</button>
$('.dd').on('click', function (e) {
e.stopPropagation();
var id = $(this).attr('data');
console.log('trying to trigger '+id);
$('#'+id).dropdown('toggle');
});
输出:
def centFromYear(year):
return (year-1)//100 +1 # substract 1 so 100 is 1 as well, floordiv by 100, add 1
for n in range(1,2000,100): # testcase centuries
for m in range(-1,2): # testcase -1,0,+1 to the centuries
print(n+m, centFromYear(n+m))