Why use the OR operator instead of the AND operator for a while loop? - or-operator

So someone has briefly explained this to me, but unfortunately I still do not understand.
My thinking is, we use an AND because this means we need both conditions to be met in order to pass.
Whereas with an OR, it only requires one condition to pass.
So how come in my example we are using an OR operator for both conditions to be met?
#DOUBLE == MEANS EQUALITY
#SINGLE = MEANS ASSIGNMENT
#THIS WILL BE THE LEGIT USER CHOICE WHERE OUR CHOICE HAS TO BE
#A NUMBER THAT IS WITHIN RANGE, SO TWO VARIABLES TO MEET BIG BOY
def my_choice ():
#VARIABLES SECTION
#INITIALS
choice = 'wrong'
accepted_range = range(1,10)
within_range = False
#Just like our choice we have to give the false answer here to keep
#the while loop- why? I dont know yet, will update
#TWO CONDITIONS TO CHECK
#1-MAKE SURE ITS AN ACTUAL NUMBER
#2-MAKE SURE ITS WITHIN THE RANGE
#CODE TIME
while choice.isdigit()==False or within_range == False:
choice = input('Please enter a value bettwen 1-9, Thanks ')
#Digit check
if choice.isdigit() == False:
print('sorry mate {} is not a digit'.format(choice))
#Range Check
#If we have passed the digit check, we can use it in our range check
if choice.isdigit() == True:
#remember that input returns a string ya?
if int(choice) in accepted_range:
within_range = True
print('Well done, {} is defintely a number in range'.format(choice))
else:
within_range = False
print('Sorry, you have picked a number, just not in range')

If you use boolean algebra you can show that these are equivalent:
is_digit or within_range
not (is_digit and within_range)
not is_digit and not within_range
You can also make a truth table to confirm this.

Related

Use walrus operator when checking value

How can I check if a variable is equal to something, and set to a new variable in the child scope?
For example:
bar = 'foobar'
my_slice = bar[:3]
if my_slice == 'foo':
print(my_slice)
It seems like the new walrus operator here would be useful, but it's not immediately straightforward how you'd go about using it here
Walrus operators work here very well, we just need to understand exactly how they work.
if (my_slice := bar[3:]) == 'foo':
print(my_slice)
Walrus operators set a variable to the output of some expression. It's almost identical to the equal sign in function except it can be used inline.
So this expression:
(my_slice := bar[3:]) == 'foo'
Can be boiled down to (variable = expression) == value
So because the output of my_slice := bar[:3] is equal to bar[:3], the above is equivalent to
bar[3:] == 'foo'
Note: The parenthesis here are required, or else variable is going to equal the output of the comparison operation, i.e. True or False

Numpy returning False even though both arrays are the same?

From my understanding of numpy, the np.equal([x, prod]) command compares the arrays element by element and returns True for each if they are equal. But every time I execute the command, it returns False for the first comparison. On the other hand, if I copy-paste the two arrays into the command, it returns True for both, as you can see in the screenshot. So, why is there a difference between the two?
You cannot compare floating-point numbers, as they are only an approximation. When you compare them by hardcoded values, they will be equal as they are approximated in the exact same way. But once you apply some mathematical operation on them, it's no longer possible to check if two floating-points are equal.
For example, this
a = 0
for i in range(10):
a += 1/10
print(a)
print(a == 1)
will give you 0.9999999999 and False, even though (1/10) * 10 = 1.
To compare floating-point values, you need to compare the two values against a small delta value. In other words, check if they're just a really small value apart. For example
a = 0
for i in range(10):
a += 1/10
delta = 0.00000001
print(a)
print(abs(a - 1) < delta)
will give you True.
For numpy, you can use numpy.isclose to get a mask or numpy.allclose if you only want a True or False value.

Raku (née Perl 6) reduce function and reduction metaoperator give different results

my #s=<1 2 3 2 3 4>;
say reduce {$^a < $^b}, #s;
say [<] #s;
# --------
# True
# False
My question is two fold:
Firstly, why does the reduction metaoperator processes the < operator differently? It looks like the reduction metaop is estimatedly using a variable which, at the first change of true to false, retains that change because:
say [\<] #s;
# ----------
# (True True True False False False)
Secondly, I'd like to use this inside the reduce function, too, i.e. introducing some code inside the curly brackets of reduce function so that it gives the same result as the reduction meta operator. How can I do it? Thank you.
Both the meta-operator and reduce take into account the associativity of the operator passed to them; in the case of <, the operator is chaining.
When passing a custom block, reduce cannot see inside of it, and so it falls back to the default of left-associative; it then compares the later values against the boolean returned from the reducer, which numifies to 0 or 1, which is why the result ends up as True in the end.
You can get the semantics you desire by declaring your reduction function as having chaining associativity:
my #s1 =<1 2 3 2 3 4>;
my #s2 =<1 2 3 4>;
sub my-reducer($a, $b) is assoc('chain') {
$a < $b
}
say reduce &my-reducer, #s1; # False
say reduce &my-reducer, #s2; # True

Multiple Boolean statements VBA

I am having issues with some Boolean logic.
Essentially I want something to program in VBA a filter such that,
A = True AND (B = True OR B = False)
I just cannot seem to get the coding right to do this in VBA (I am using MS Access).
I have tried:
A = True AND B = True OR B = False
But this obviously fails (looks for either A,B = True OR B = False, essentially).
Am I missing something obvious here?
I chose to left out the actual code, but if requested I can post. My thinking is that I am missing some basic with the Boolean logic.
If A, B, and C are Boolean expressions, then comparing them to a Boolean literal (True, False) is redundant: the expression is already a Boolean expression.
That makes the original logic go like this:
If A And (B Or Not C) Then
The And operator has greater priority than the Or operator, so it gets evaluated first; if the (B Or Not C) part needs to be evaluated as a whole, then the parentheses are required, otherwise A And B takes precedence, making the expression (wrongly) evaluate as:
If (A And B) Or Not C ' redundant parentheses to illustrate operator precedence
Edit: I misread the OP, my brain put that C in there. If A And (B Or Not B) Then is redundant, as BigBen pointed out - the simplified logic would be If A Then. Still worth noting operator precedence and using parentheses when applicable and appropriate though.

Using two equals signs in Visual Basic 2008

In code, why wouldn't this work?
intMax = intTopValue = 20
This is interpreted as intMax = (intTopValue = 20).
intTopValue = 20 will check whether intTopValue is equal to 20 and return true or false.
This boolean will then be assigned to intMax.
Most languages don't have this issue, since they use separate operators for assignment (= or :=) and equality (== or =).
By contrast, VB shares = for both operations. Therefore, when a = b is written as an expression, it always means equality.