1、not or and 的优先级是不同的:not > and > or
not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 and 8 and 9
按照优先级给它们加上括号:(not 1) or (0 and 1) or (3 and 4) or (5 and 6) or (7 and 8 and 9)
== 0 or 0 or 4 or 6 or 9
== 4
2、还记得那个求闰年的作业吗?如果还没有学到“求余”操作,还记得用什么方法可以“委曲求全”代替“%”的功能呢?
“if year/400 == int(year/400)”
可以改为
if year/400 % == 0
3、写一个程序打印出 0~100 所有的奇数。
我的答案:
for i in range(100):
if i%2 != 0:
print(i)
(bug搞了半天,用了除号‘/’,应该是取余%)
参考答案:
i = 0
while i <= 100:
if i % 2 != 0:
print(i, end=' ')
i += 1
else:
i += 1
4、爱因斯坦的难题
有一个长阶梯,若每步上2阶,最后剩1阶;若每步上3阶,最后剩2阶;若每步上5阶,最后剩4阶;若每步上6阶,最后剩5阶;只有每步上7阶,最后刚好一阶也不剩。
题目:请编程求解该阶梯至少有多少阶?
i=1
while(True):
if (i % 2 ==1 and i % 3 ==2 and i % 5 ==4 and i % 6 == 5 and i % 7 ==0):
print(i)
break
else:
i+=1
坑爹了,没有打break,死循环了!