python不相等的两个字符串的 if 条件判断为True

版权声明:本文为博主原创文章,转载请注明出处 https://blog.csdn.net/weixin_35757704/article/details/88232489

今天遇到一个非常基础的问题,结果搞了好久好久.....赶快写一篇博客记录一下:

本来两个不一样的字符串,在if 的条件判断中被判定为True,下面是错误的代码:

test_str = 'happy'
if test_str == 'good' or 'happy': #这样if判断永远是True,写法错误
    print('aa')
else:
    print('bbbb')

这是正确的代码:

test_str = 'happy'
if test_str == 'good' or test_str == 'happy':
    print('aa')
else:
    print('bbbb')

猜你喜欢

转载自blog.csdn.net/weixin_35757704/article/details/88232489
今日推荐