Something very strange happens to me, that when putting the following code with Jupyter:
x=int(input("Escribe un número: "))
if x>0 and x<10:
print("Has escrito un número en el rango de [0,10]")
The same thing happens to me when I put and
andor
Thank you!!!
Problem?
What you have is not an error, it is simply the result of the logical operators that you have applied (
and
,or
) for that we can be guided by a truth table.In the proposition "and" (
^
) we are told that it will be true when both values are true, while in the proposition "or" (v
) it is said that it will be true when one of its values is true.Turning that into programming there are the values falsy and truthy which exist to define a variable as boolean or boolean , as a quick example.
It is evaluated as
False
:None
False
0
(either an integer or a float number)It is evaluated as
True
True
your "problem"
As I said, it is not a problem in itself since what happens is that you have not known how to use a correct expression, let's see in detail what it means and what happens with each of the expressions and operators.
First, remember that expressions are evaluated from left to right, having said that, let's see what happens.
and
It
and
will evaluate if both conditions are met, in which case it returnsTrue
, if the first condition is evaluated asFalse
, the second will no longer be evaluated. For your example the following is validated:or
With
or
it is evaluated if only one of the expressions or values isTrue
, if one of the conditions isTrue
returnedTrue
, for the example the following is validated:There is something called "short-circuit evaluation" , this means that only the necessary expressions are evaluated, for example in the case of
and
:Solution
If what you want is to validate that the entered number is in a range, you can use the function
range(inicio,fin)
If something is not clear to you, let me know and I will try to explain it.