Im trying make a condition in excel VBA where there are two posible condition of variable x and y,
Say for example
Sub Test()
X = 6
Y = 11
If X < 3 Or X > 5 And Y < 10 Then
X = 10
Else
X = 11
End If
MsgBox X
End Sub
For the X term, when X<3 or X>5 seems work well however when i change the Y to any value greater than 10 say 11 then the result is 10 but supposed to be it should be 11. Can you please let me know if i missing something in my code so that when X<3 or X>5 and (y=11)>10 the result must be 11.
Regards,
Kenneth
This is an issue of operator precedence. In VB the order in Logical and Bitwise Operators is as follows:
Negation (Not)
Conjunction (And, AndAlso)
Inclusive disjunction (Or, OrElse)
Exclusive disjunction (Xor)
The result is that and is executed before or in your condition, leading to the right side of the or being the whole expression X > 5 And Y < 10.
Your condition is executed like:
If X < 3 Or (X > 5 And Y < 10) Then
I believe what you actually want is the following (this should solve your issue):
If (X < 3 Or X > 5) And Y < 10 Then
Sources:
MSDN: Operator Precedence in Visual Basic
Consider:
Sub Test()
X = 6
Y = 11
If (X < 3 Or X > 5) And Y < 10 Then
X = 10
Else
X = 11
End If
MsgBox X
End Sub
Related
I have a question ,what is the meaning of "range( i + 1 )" below ,if I want to show the output which has xyyzzz?
a = ("x", "y", "z")
for i in range(len(a)):
for j in range( i + 1):
print(a[i])
output: x
y
y
z
z
z
I tried to explain this to you with values
i = 0 ---> j = range(1) = 0 : a[0] = x
--------------------------------
i = 1 ----> j =range(2) = (0,1) : a[1] = y
: a[1] = y
-----------------------------------------
i = 2 ----> j = range(3) = (0,1,2) : a[2] = z
: a[2] = z
: a[2] = z
range(n) gives you values from 0 to n-1.
If you are unfamiliar with Python currently, this can be represented in different ways:
In mathematical interval notation, you can represent this as [0,n) . The left value is included, and the right one is excluded.
Taking len(a) to be 3, the above for loops would be written in C as:
for (int i = 0 ; i < 3 ; ++i){
for (int j = 0 ; j <= i ; ++j){ // note the <= . We can also do j < (i+1)
...
}
}
Your code first calculates len(a), which is 3, since this is a container with 3 elements (this specific container is called a tuple). The first for loop goes from 0 to 2, while the second for loop goes from 0 to wherever the counter of the outer for loop is at (less than or equal to). This causes the first value to be printed just once, next twice, and the last one thrice.
(If you want to test your understanding further, try to figure out what would be printed if we had print(a[j]) inside the loops rather than a[i].)
range() is a versatile and powerful thing in Python, it can do much more than just give you values from 0 to n-1. Do read about it if you intend to use Python.
[Sorry in advance, I'm very new to programming.]
This is for project euler problem #2. The goal is to calculate the sum of all even fibonacci numbers that do not exceed 4 million. For anyone not familiar with fibonacci numbers, a fibonacci number is simply a number that is the sum of the two previous numbers in the sequence. For example, the first few fibonacci numbers are 1,2,3,5,8,13,21,34 ...
My code is below beginning with some variables, then my while loop, and finally my for loop.
n = 0
n2 = 1
fibsum = 0
fibrange = range(1,4000001)
while (n2 <= 4000000):
n2 = n2 + n
n = n2 - n
if n2 % 2 == 0:
fibsum += n2
print (fibsum)
# for n2 in fibrange:
# n2 = n2 + n
# n = n2 - n
# if n2 % 2 == 0:
# fibsum += n2
# print(fibsum)
As I said, my while loop works like a charm, but when I run the for loop the output of fibsum is 0. So the value is not changing at all.
I've tried range (1, 4000001) in place of fibrange. I really have no idea what else to try. This is like my 4th or 5th program ever.
Using for loop in range function, The Value of the variable n2 changes according only to range function.You cannot manually change the variable in for loop while using range function.
for and while and are different types of loops.
while: Repeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body.
for: Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
With while you can do something like this:
a = 0
b = 1
target = 4000000
result = 0
while a <= target:
if a % 2 == 0:
result +=a
a, b = b, a + b
print(result)
With for:
a, b = 0, 1
result = 0
target = 4000000
fib_sequence = 35 # -> the length of fibonacci sequence
for _ in range(fib_sequence):
if a % 2 == 0:
result +=a
a, b = b, a + b
if a >= target: break
print(result)
How does a nested loop work?
I tried to make a triangle this is my code:
x = 0
string = ""
while x <= 5:
y = x
while y > 0:
string = string + "*"
y = y - 1
string = string + "\n"
x = x + 1
print(string)
But it came out like this And this is the output:
*
**
***
****
*****
How does a nested loop work in python?
You must debug your code to understand how the nested loop works
for your code, the nested loop will work as following,
for each x where x= 5 the while loop of y will run for 5 times,
then again,
when x=4 the while loop of y will run for 4 times,
and as per this the while loop of x will have 5 iterations and the while loop of y will have about 15 iterations equivalent to number of stars in your pyramid or pattern.
I have such a binary search to find square root of a positive integer
In [95]: find_square_root??
Signature: find_square_root(x)
Docstring: <no docstring>
Source:
def find_square_root(x):
if x < 2:
return x
lo = 0
hi = x
while lo < hi:
mid = (lo + hi) // 2 #
if mid ** 2 == x:
lo = mid
return lo
if mid ** 2 < x:
lo = mid + 1
if mid ** 2 > x:
hi = mid
print(f"mid={mid}, lo={lo}, hi={hi}")
return lo -1
File: /tmp/ipython_edit_um5dfgck/ipython_edit_sdk9u57g.py
Type: function
I tested up to 50**5 cases which works properly
for i in range(50, 50**5):
res = find_square_root(i**2)
assert res == i, f"res={res}, i={i}"
However, there exist a logic bug there roughly.
Suppose only two numbers left finally, lo and hi which are adjacent to each other surely and they are not tested yet.
According to the algorithms, mid = (lo + hi) // 2, since it floor division, mid is actually equals to lo, so one of the left two number is tested,
additionally if mid ** 2 > x:, then hi = mid = lo,
this way, the function quit safely.
However, if if mid ** 2 < x:, then lo = mid + 1 which means lo = hi and the loop quit with hi left untested.
It seems like a solid logic bug.
but I am not sure because it passed mass of testings.
However, if mid ** 2 < x:, then lo = mid + 1 which means lo = hi and the loop quit with hi left untested.
It seems like a solid logic bug.
There is no bug here. hi is not left untested, as hi is the value that mid had in a previous iteration, when hi = mid was executed, and so it was already tested there.
If there was never such previous iteration where hi was modified, then this means hi equals x, and that in each iteration mid ** 2 < x is true, until and including when mid == x - 2 (which would assign li = x - 1). But this can only happen when x < 4. In those few cases the solution is 1. As we know x > 1 after the first if, we have hi > 1, so no problem for those cases either. You could even change the first if to if x < 4...
The way to think about it is that hi is one past the end of the range you're searching. So the numbers being searched are the range lo to hi-1, and there is never any need to test hi. Once lo == hi, the range is empty so the number wasn't found, and there is no need to continue testing. So there is no logic error in this code.
I've been practicing Haskell as part of my course at my university, and I made the following function while experimenting with local definitions :
fac1 x | x == 0 = zero
| x == 1 = one
| otherwise = x * fac1 (x-1)
where zero = 0
one = 1
I would expect any call to fac1 result in zero because when x==0, it will multiply by 0. However, it gives me the correct number.
Conversely, writing one = 0 instead of one = 1 results in my results being 0. I would expect the same sort of behavior for zero, but changing the value of it does nothing. I feel like it should be happening since I clearly included a x==0 condition. The x==1 is evaluated, why not x==0?
Can someone explain what error I made?
Your recursion stops on x = 1, not x = 0.
Let's evaluate fac1 2 by hand:
fac1 2
= 2 * fac1 (2 - 1) -- 2 is neither 0 nor 1, so we take the "otherwise" case
= 2 * fac1 1 -- evaluate 2 - 1
= 2 * one -- x == 1 holds, so we return "one"
= 2 * 1 -- one is 1
= 2 -- done