我似乎无法掌握Bootlean运营商。我正在使用Code Academy的一个例子。起初我读错了,并且放True or not False and False
而不是True or False
。
有人可以为我更清楚地解释一下,这样我就可以获得更多的理解。
Assign True or False as appropriate for bool_one through bool_five.
Set bool_one equal to the result of False or not True and True
Set bool_two equal to the result of False and not True or True
Set bool_three equal to the result of True and not (False or False)
Set bool_four equal to the result of not not True or False and not True
Set bool_five equal to the result of False or not (True and True)
答案 0 :(得分:1)
您有三个布尔操作和一些规则:
not
,它只是反转(又名not True
=> False
,not False
=> True
)or
,适用于两个操作数x or y
,如果True
和True
同时为False
,则返回False
< / LI>
and
,对两个操作数x and y
起作用,如果True
和True
都为False
,则返回not
and
具有最高优先级,之后为or
,最后()
<pre>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> - bar chart demo</title>
<script type='text/javascript' src='http://d3js.org/d3.v3.min.js'></script>
<style type='text/css'>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: orange;
}
.bar:hover {
fill: orangered ;
}
.x.axis path {
display: none;
}
.d3-tip {
line-height: 1;
font-weight: bold;
padding: 12px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
border-radius: 2px;
}
/* Creates a small triangle extender for the tooltip */
.d3-tip:after {
box-sizing: border-box;
display: inline;
font-size: 10px;
width: 100%;
line-height: 1;
color: rgba(0, 0, 0, 0.8);
content: "\25BC";
position: absolute;
text-align: center;
}
/* Style northward tooltips differently */
.d3-tip.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 0;
}
</style>
<script type='text/javascript'>//<![CDATA[
window.onload=function(){
}//]]>
</script>
</head>
<body>
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
<script>
var margin = {top: 40, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var formatPercent = d3.format(".0%");
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(formatPercent);
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return "<strong>Frequency:</strong> <span style='color:red'>" + d.frequency + "</span>";
})
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.call(tip);
// The new data variable.
var data = [
{letter: "A", frequency: .08167},
{letter: "B", frequency: .01492},
{letter: "C", frequency: .02780},
{letter: "D", frequency: .04253},
{letter: "E", frequency: .12702},
{letter: "F", frequency: .02288},
{letter: "G", frequency: .02022},
{letter: "H", frequency: .06094},
{letter: "I", frequency: .06973},
{letter: "J", frequency: .00153},
{letter: "K", frequency: .00747},
{letter: "L", frequency: .04025},
{letter: "M", frequency: .02517},
{letter: "N", frequency: .06749},
{letter: "O", frequency: .07507},
{letter: "P", frequency: .01929},
{letter: "Q", frequency: .00098},
{letter: "R", frequency: .05987},
{letter: "S", frequency: .06333},
{letter: "T", frequency: .09056},
{letter: "U", frequency: .02758},
{letter: "V", frequency: .01037},
{letter: "W", frequency: .02465},
{letter: "X", frequency: .00150},
{letter: "Y", frequency: .01971},
{letter: "Z", frequency: .00074}
];
// The following code was contained in the callback function.
x.domain(data.map(function(d) { return d.letter; }));
y.domain([0, d3.max(data, function(d) { return d.frequency; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Frequency");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.letter); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.frequency); })
.attr("height", function(d) { return height - y(d.frequency); })
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
function type(d) {
d.frequency = +d.frequency;
return d;
}
</script>
</body>
</html>
</pre>
更改操作的优先级,就像在日常数学中一样答案 1 :(得分:0)
5.15 Operator Precendence(列出从最低到最高,我修剪了许多操作员)
or
and
not
(expressions...)
因此,您可以看到从低到高的优先级为or
然后是and
,然后是not
,然后是()
。实施例
False or not True and True
所以如果我添加括号来强调顺序
(False or (not True)) and (True)
变为
(False or False) and (True)
False and True
False
您可以使用其他行
执行此过程答案 2 :(得分:0)
布尔变量可以包含两个值之一 - True
或False
。有几个运算符可以帮助您操作布尔值:
not
反转了价值 - True
变为False
,反之亦然and
当且仅当两个操作数都评估为True
True
如果其中一个或哪些操作数评估为or
,则True
会返回True
本练习要求您将英语语句翻译为python:
bool_one = False or not True and True
bool_two = False and not True or True
bool_three = True and not (False or False)
bool_four = not not True or False and not True
bool_five = False or not (True and True)