Python语句中的布尔逻辑

Python语句中的布尔逻辑,python,boolean,boolean-logic,Python,Boolean,Boolean Logic,我的程序适用于除“3 2 4 False”之外的所有组合 我不明白为什么这个组合的结果是真的。第一个闭合集应该返回False,因为bOk=False,第二个闭合集也应该返回False,因为b>a为False 如能解释,将不胜感激 布尔值是两个常量对象False和True 对于布尔字符串 # Given three ints, a b c, print True if b is greater than a, # and c is greater than b. However, with t

我的程序适用于除“3 2 4 False”之外的所有组合

我不明白为什么这个组合的结果是真的。第一个闭合集应该返回False,因为bOk=False,第二个闭合集也应该返回False,因为b>a为False


如能解释,将不胜感激

布尔值是两个常量对象False和True

对于布尔字符串

# Given three ints, a b c, print True if b is greater than a,  
# and c is greater than b. However, with the exception that if 
# "bOk" is True, b does not need to be greater than a. 

a = int(input())
b = int(input())
c = int(input())
bOk = bool(input())

print(((bOk and c > b) or (b > a and c > b)))
bool检查列表是否有对象。如果为空,则返回False;如果为空,则返回True


在您的例子中,bOk=bool(input())有一个值,因此无论对象是什么,bOk都返回True。因此,您的输出。

布尔值是两个常量对象False和True

对于布尔字符串

# Given three ints, a b c, print True if b is greater than a,  
# and c is greater than b. However, with the exception that if 
# "bOk" is True, b does not need to be greater than a. 

a = int(input())
b = int(input())
c = int(input())
bOk = bool(input())

print(((bOk and c > b) or (b > a and c > b)))
bool检查列表是否有对象。如果为空,则返回False;如果为空,则返回True


在您的例子中,bOk=bool(input())有一个值,因此无论对象是什么,bOk都返回True。因此,您的代码在Python2.x上工作,因为在Python2.x中,input()等于eval(raw_input(prompt))

但是在Python3.x中,input()等于raw_input(),所以bOk等于bool(“False”),等于True

>>> a=int(input())
3
>>> b=int(input())
2
>>> c=int(input())
4
>>> bOk=bool(input())
False
>>> print(bOk and c > b)
False
>>> bOk
False
>>> print(((bOk and c > b) or (b > a and c > b)))
False
您可以将input()更改为eval(input())

“Python 2到3转换工具将用eval(input())替换对input()的调用,用input()替换对raw_input()的调用。”


请参阅

您的代码在Python2.x上工作,因为在Python2.x中,input()等于eval(raw_input(prompt))

但是在Python3.x中,input()等于raw_input(),所以bOk等于bool(“False”),等于True

>>> a=int(input())
3
>>> b=int(input())
2
>>> c=int(input())
4
>>> bOk=bool(input())
False
>>> print(bOk and c > b)
False
>>> bOk
False
>>> print(((bOk and c > b) or (b > a and c > b)))
False
您可以将input()更改为eval(input())

“Python 2到3转换工具将用eval(input())替换对input()的调用,用input()替换对raw_input()的调用。”


关于OPs错误的解释,请参阅前面已经包含的注释和其他答案。我更愿意展示在更受限制的环境中(生产?)通常是如何完成事情的

代码没有经过充分测试,也不是最优雅的,尽管重点是:清理输入。总是。并以完全不同的方式提示用户选择问题(是/否,对/错)

在下面的示例中,bool提示符被净化为“此值是唯一被视为True的值;所有其他值均为False”


希望这对如此庞大的上市有所帮助,并表示歉意。处理用户输入始终是一项棘手的任务。

评论和其他答案已经涵盖了OPs错误的解释。我更愿意展示在更受限制的环境中(生产?)通常是如何完成事情的

代码没有经过充分测试,也不是最优雅的,尽管重点是:清理输入。总是。并以完全不同的方式提示用户选择问题(是/否,对/错)

在下面的示例中,bool提示符被净化为“此值是唯一被视为True的值;所有其他值均为False”


希望这对如此庞大的上市有所帮助,并表示歉意。处理用户输入总是一项棘手的任务。

bool(“False”)
不等于
False
<代码>“False”是一个非空字符串,因此它的计算结果为
True
。也许
bOk=(input().lower()='True')
就足以满足您的需要了。@khelwood听起来像是一个可靠的答案。@asongtoruin Daniel Roseman的评论实际上回答了“非常感谢解释”的问题。
bool(“False”)
不等于
False
<代码>“False”是一个非空字符串,因此它的计算结果为
True
。也许
bOk=(input().lower()='True')
就足以满足您的需要了。@khelwood听起来像是一个可靠的答案。@asongtoruin Daniel Roseman的评论实际上回答了“请解释”的问题。
#!/usr/bin/env python
"""Just an example."""


def input_type(prompt, type_):
    """Prompt a user to input a certain type of data.

    For a sake of simplicity type_ is limited to int and str.

    :param prompt: prompt message to print
    :param type_:  type of a data required

    :type prompt:  str
    :type type_:   int or bool

    :return: data of a required type retrieved from STDIN
    :rtype:  type_
    """
    accepted_types = [int, str]
    if isinstance(prompt, str):
        if any(type_ == atype for atype in accepted_types):
            while True:
                user_input = input(prompt)
                try:
                    return type_(user_input)
                except ValueError:
                    continue
        else:
            errmsg = 'Requested type is unsupported by this function: %s'
            errmsg = errmsg % type_.__name__
    else:
        errmsg = 'Prompt must be a string: got %s instead'
        errmsg = errmsg % type(prompt).__name__

    raise Exception(errmsg)


def input_bool(prompt, as_true):
    """Prompt user to answer positively or negatively.

    :param prompt:  prompt message to print
    :param as_true: value to interpret as True

    :type prompt:  str
    :type as_true: str

    :return: user answer
    :rtype:  bool
    """
    if isinstance(as_true, str):
        return input_type(prompt, str) == as_true
    else:
        errmsg = "'as_true' argument must be a string: got %s instead"
        errmsg = errmsg % type(as_true).__name__

    raise Exception(errmsg)


if __name__ == '__main__':
    a = input_type('Enter first integer: ', int)
    b = input_type('Enter second integer: ', int)
    c = input_type('Enter third integer: ', int)
    bOk = input_bool('Enter boolean value (True/False): ', 'true')

    # Result
    print(((bOk and c > b) or (b > a and c > b)))