简体   繁体   English

为什么我的功能无法打印

[英]Why won't my function print

So I have this function. 所以我有这个功能。

def test(fourBits):
  binaryDigits = fourBits
  if binaryDigits[1] == 1:
     print 'test'

If I enter test('1111') it won't print test . 如果我输入test('1111') ,则不会打印test I don't understand why it is not? 我不明白为什么不是这样?

binaryDigits [1]是字符串,1是整数

Use this: 用这个:

if int(binaryDigits[1]) == 1:

or this: 或这个:

if binaryDigits[1] == '1':

so that the types match, ie, compare two characters or two numbers. 以便类型匹配,即比较两个字符或两个数字。

Perhaps what you want is something like this. 也许您想要的就是这样。 Use integer instead of string, and test the bit by bitwise operators. 使用整数而不是字符串,并通过按位运算符测试该位。

def test(value):
    if (value >> 1) & 1:
        print 'true'

Here is the result. 这是结果。

>>> test(0b0010)
true
>>> test(0b0000)
>>>

尝试print binaryDigits[1]之前,您if语句来看看什么是由你的隐藏if语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM