我最近开始学习我的第一门编程语言,我不明白为什么这段代码给了我A而不是B
if 3 == 1 or 2:
print('A')
else:
print('B')
答案 0 :(得分:1)
您检查两个条件var myGamePiece;
var myObstacle;
var platformWidth = 1500;
var platformX = 0;
var platformY = 630;
function startGame() {
myGameArea.start();
myGamePiece = new Component(22, 22, "red", 30, 30);
}
var myGameArea = {
canvas: document.createElement("canvas"),
start: function() {
this.canvas.width = platformWidth;
this.canvas.height = platformY;
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
this.interval = setInterval(updateGameArea, 20);
window.addEventListener("keydown", function(e) {
myGameArea.key = e.keyCode;
});
window.addEventListener("keyup", function(e) {
myGameArea.key = false;
});
},
fill: function(color) {
this.context.save();
this.context.fillStyle = "white";
this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
this.obstacle = function() {
this.context.fillStyle = "green";
this.context.fillRect(0, 600, 1505, 30);
};
this.obstacleWall = function() {
this.context.obstacleWallWidth = 20;
this.context.obstacleWallHeight = 300;
this.context.obstacleWallX = 300;
this.context.obstacleWallY = 300;
this.context.fillStyle = "red";
this.context.fillRect(
this.context.obstacleWallX,
this.context.obstacleWallY,
this.context.obstacleWallWidth,
this.context.obstacleWallHeight
);
};
this.context.restore();
},
clear: function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
};
function Component(width, height, color, x, y, type) {
this.type = type;
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.speedX = 0;
this.speedY = 0;
this.gravity = 0.05;
this.gravitySpeed = 0;
this.update = function() {
ctx = myGameArea.context;
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
this.obstacle = function() {
this.fillStyle = "green";
this.fillRect(0, 600, 600, 30);
};
};
this.newPos = function() {
this.gravitySpeed += this.gravity;
this.x += this.speedX;
this.y += this.speedY + this.gravitySpeed;
this.hitBottom();
this.collideWall();
};
this.collideWall = function() {
var collide = myGameArea.obstacleWall.width - this.width;
if (this.x > collide) {
this.speedX = 0;
}
};
this.hitBottom = function() {
var rockbottom = myGameArea.canvas.height - 30 - this.height;
if (this.y > rockbottom) {
this.y = rockbottom;
if (
myGameArea.canvas.height - this.height &&
myGamePiece.key == 38
) {
this.y = this.speedY;
}
this.gravitySpeed = 0; // reset?
}
};
}
function updateGameArea() {
myGameArea.fill("#DE7");
myGameArea.obstacle();
myGameArea.obstacleWall();
myGamePiece.speedX = 0;
myGamePiece.speedY = 0;
if (myGameArea.key) {
switch (myGameArea.key) {
case 37: // left arrow
myGamePiece.speedX = -5;
break;
case 38: // up arrow
myGamePiece.speedY = -5;
break;
case 39: // right arrow
myGamePiece.speedX = +5;
break;
case 40: // down arrow
default:
myGamePiece.speedY = +5;
}
}
myGamePiece.newPos();
myGamePiece.update();
myObstacle.update();
}
startGame();
和3==2
。 2
的值为2
,因此您得到true
作为输出。
如果要检查蜂1或2的3,则必须这样做:
A
例如参见Do not understand why if condition evaluates to True when variable is integer详细了解为什么2为真,尤其是this答案。
答案 1 :(得分:0)
在python中,它可以读为if 3==1 or bool(2) == True
,其中第一个条件为False,第二个条件为True,因为bool(2)
为True
答案 2 :(得分:0)
您有一个错误的假设,即3 == 1 or 2
的意思是“ 3等于1或2?”,但实际上是“ 3等于1或2求值为True”,并且因为2是总是 计算为True
的非零值。
更具体地说,3 == 1 or 2
表达式的计算结果为False or 2
,然后是False or True
,最后是True
。
如果要使表达式与原始期望值匹配,则应编写3 in [1, 2]
来检查列表中允许的值之一是否为3。