python if 'a' and 'b' and 'c' in 'abc'的坑

需求:想判断‘a’ ,‘b’ ,‘c’ 三个字符串都在‘abc’里在执行下条语句。
下面代码是错误的


    if 'a' and 'b' and 'c' in 'abc' :
    	print 'right'
    else :
    	print 'Error'	

正确写法

if 'a'  in 'abcd' and 'b' in 'abcd'  and 'c'  in 'abc' :
	print 'right'
else :
	print 'Error'	

原因:python中if判断条件是判断语句是否为true,什么条件为true?有字符时就为true(除了bool类型的false)。
按第一种写法这种情况是判断不出来的。

if 'xxxxxxxxxxxx' and 'ababababddddddddddd' and 'c' in 'abc' :
	print 'right'
else :
	print 'Error'	

执行结果是:right,因为只判断了 ‘c’ in ‘abc’。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值