【易错点】python新手容易搞不懂的知识点(三):常用操作符

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,死循环了!

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值