我在python中包含了以下条件,尽管我将x设置为5并且第一个条件是False,但我仍然收到测试1的输出
x = 5
if [(( 0 < x <= 1.084) | ((x) == 0))]:
print((( 0 < x <= 1.084) | ((x) == 0)))
print('Test1')
elif [(1.084 < x <= 5)]:
print('Test2')
False
Test1
答案 0 :(得分:3)
问题是您的条件周围有方括号,这会将条件的评估( <?php require_once("../includes/connection.php");
$connection = sqlsrv_connect(DB_HOST, array( 'Database'=>DB_NAME, 'UID'=>DB_USER, 'PWD'=>DB_PASSWORD,"CharacterSet" => "UTF-8"));
// /* Database connection end */
if( $connection ) {
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
// // storing request (ie, get/post) global array to a variable
$requestData= $_REQUEST;
$columns = array(
// datatable column index => database column name
0 =>'new_id',
1 => 'person_name',
2=> 'given_name',
3=> 'family_name'
);
// getting total number records without any search
$sql = "SELECT * , user.id as new_id ";
$sql.=" FROM user LEFT JOIN user_additional on user.doc_id = user_additional.doc_id LEFT JOIN users on users.id = user.user_name";
$query=sqlsrv_query($connection, $sql) ;
if( $query === false) {
die( print_r( sqlsrv_errors(), true) );
}
$totalData = sqlsrv_num_rows($query);
$totalFiltered = $totalData; // when there is no search parameter then total number rows = total number filtered rows.
$sql ="SELECT * , user.id as new_id ";
$sql.=" FROM user LEFT JOIN user_additional on user.doc_id = user_additional.doc_id LEFT JOIN users on users.id = user.user_name WHERE 1=1";
if( !empty($requestData['search']['value']) ) { // if there is a search parameter, $requestData['search']['value'] contains search parameter
$sql.=" AND ( person_name LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR family_name LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR given_name LIKE '".$requestData['search']['value']."%' )";
}
$query=sqlsrv_query($connection, $sql) ;
if( $query === false) {
die( print_r( sqlsrv_errors(), true) );
}
$totalFiltered = sqlsrv_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result.
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." ";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc */
$query=sqlsrv_query($connection, $sql) ;
if( $query === false) {
die( print_r( sqlsrv_errors(), true) );
}
$data = array();
while( $row=sqlsrv_fetch_array($query) ) { // preparing an array
$nestedData=array();
// $nestedData[]="";
$nestedData[] = $row["new_id"];
$nestedData[] = $row["person_name"];
$nestedData[] = $row["given_name"];
$nestedData[] = $row["family_name"];
;
$data[] = $nestedData;
}
$json_data = array(
"draw" => intval( $requestData['draw'] ), // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw.
"recordsTotal" => intval( $totalData ), // total number of records
"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
"data" => $data // total data array
);
echo json_encode($json_data); // send data as json format
?>
)放入Python列表(False
)中。
只要列表中有任何元素,Python列表在布尔上下文中都将评估为true,因此[False]
是循环的有效true条件。
要修复此问题,只需除去括号,括号将使[False]
看上去if
,而不是列表。
此外,您可能想使用False
而不是or
,因为前者是布尔值的常用运算符,后者是整数或的位。
您还可以摆脱很多诱因,Python通常不需要它们。
修改后的代码:
|
答案 1 :(得分:2)
不要在括号中使用方括号。它们不可互换。并对逻辑或使用or
,而不对按位OR使用|
。
if ((0 < x <= 1.084) or (x == 0)):
请注意,实际上不需要这些括号。
if 0 < x <= 1.084 or x == 0:
如果将<
更改为<=
,则不需要第二次检查。
if 0 <= x <= 1.084:
答案 2 :(得分:2)
除了John的回答,此代码仅是:
if 0 <= x <= 1.084:
print('Test1')
elif 1.084 < x <= 5:
print('Test2')
就是这样。