A confusion with while loop - while-loop

ConfirmationforNewDeviceName= str(input("Please insert 'Yes' or 'No' :"))
while (ConfirmationforNewDeviceName != "Yes" or
ConfirmationforNewDeviceName != "yes" or
ConfirmationforNewDeviceName != "No" or
ConfirmationforNewDeviceName != "no"):
ConfirmationforNewDeviceName= str(input("Please insert 'Yes' or 'No' :"))
This is my program in Python 3 . I want that the loop end only if I get Yes, yes, No, or no of string in line 1 otherwise it continue to run. But in given program i have created the loop didn't end whatever i write. It continue to show the "Please insert 'Yes' or 'No' :" of the line 7. Please help me to overcome this.

You need ands, not ors.
If ConfirmationforNewDeviceName is "yes", think about this:
"yes" != "Yes" or "yes" != "yes"
That evaluates to True or False, which evaluates to True. On the other hand:
"yes" != "Yes" and "yes" != "yes"
evaluates to True and False, which evaluates to False as expected.

Related

How can I use two conditions for my `IF` block?

I have a problem to create double conditions with If, so this is what I have for hiding a line:
If Target.Address = "$D$35" Then
If (Target.Value = "No") Then
ActiveSheet.Range("37:40").Rows.Hidden = False
End If
If (Target.Value = "Yes") Then
ActiveSheet.Range("37:40").Rows.Hidden = True
End If
End If
Now, I want to add another condition for this formula, let's say that that cell $D$35 need to be "No" and Cell $D$36 need to be "done" in order for the Hide function to work.
How do I insert this to my existing formula ?
1. You can use And for multiple conditions (cf #jonrsharpe comment). Actually take a look at other logical operators like Or etc ... Logical and Bitwise Operators
2. Why don't you use If\ElseIf\Else ? It's quite obvious that if Target.Value = "No" is true you do not have to test if Target.Value = "Yes" ...
So just do something like that:
If cond1 And cond2 Then
' Some code
ElseIf cond3 And cond4 Then '<--editted
' Some code
Else
' Some code
End If
Example: Let's say you want to do a condition on the values of cell D35 ($D$35) and D36 you can just do:
If Range("D35")= "No" And Range("D36")= "No" Then
ActiveSheet.Range("37:40").Rows.Hidden = False
ElseIf Range("D35")= "Yes" And Range("D36")= "done" Then
ActiveSheet.Range("37:40").Rows.Hidden = True
End If
Instead of
If Target.Address = "$D$35" Then
If (Target.Value = "No") Then
ActiveSheet.Range("37:40").Rows.Hidden = False
End If
If (Target.Value = "Yes") Then
ActiveSheet.Range("37:40").Rows.Hidden = True
End If
End If
Note 1: this assumes you are working on the right sheet
Note 2: this isn't especially the best way to do ...
First, it's not a formula, it's a subroutine. Second, you don't have to have multiple IF blocks. Just one should suffice.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("D35:D36")) Is Nothing Then
ActiveSheet.Range("37:40").Rows.Hidden = (Range("D35").Value = "No" And Range("D36").Value = "Done")
End If
End Sub
A little explanation is in order:
If Not Intersect(Target, Range("D35:D36")) Is Nothing returns TRUE if the change is detected in either D35 or D36. If you want the condition to check in non-contiguous ranges, use Union on the ranges you want to check.
(Range("D35").Value = "No" And Range("D36").Value = "Done") evaluates to TRUE if both conditions are met, FALSE if else. We then use this as the boolean trigger for the Hidden property of the given range.
Hope this helps.

Unwanted data showed by crystal report

i'am using this formula for parameters (record selection) :
if {?customPort} <> "" then {ArrivingTrips.port} = {?customPort};
if {?airlin} <> "" then {ArrivingTrips.airlines} = {?airlin};
if {?hal} <> "" then {ArrivingTrips.hall}={?hal};
if (hasvalue({?tim})) then {ArrivingTrips.arrivalTime}={?tim};
{?d1} = {ArrivingTrips.arrivalDate};
the problem now, crystal report just looks at d1 and dosent care for another parameters even if the "if" expression is true so it shows unwanted data, why that ?
is it wrong syntax ?
Note : i send the parameters values by my vb.net code, not by Crystal parameters input panel.
Thanks.
local timevar t;
local stringvar air;
local stringvar port;
local stringvar halll;
if {?tim} <> "" then t:= ctime({?tim}) else t:= ctime({ArrivingTrips.arrivalTime});
if {?airlin} <> "" then air:= {?airlin} else air:= {ArrivingTrips.airlines};
if {?customport} <> "" then port:= {?customPort} else port:= {ArrivingTrips.port};
if {?hal} <> "" then halll:= {?hal} else halll:= {ArrivingTrips.hall};
{ArrivingTrips.airlines} = air and {?d1}= {ArrivingTrips.arrivalDate} and port={ArrivingTrips.port} and halll = {ArrivingTrips.hall} and (t)=ctime({ArrivingTrips.arrivalTime});
This is the solution, now , when "if condition" returns false result, report will care only for true conditions because the false one will will return the same value of a field so it's like no condition :)
Thanks for every one tried to help.
({?d1} = {ArrivingTrips.arrivalDate})
AND (If {?customPort} <> "" Then {ArrivingTrips.port} = {?customPort} Else TRUE)
AND (If {?airlin} <> "" Then {ArrivingTrips.airlines} = {?airlin} Else TRUE)
AND (If {?hal} <> "" Then {ArrivingTrips.hall}={?hal} Else TRUE)
AND (If (hasvalue({?tim})) Then {ArrivingTrips.arrivalTime}={?tim} Else TRUE)
You are writing your selection formula as single statements, so only the last statement in line gets evaluated. You should also have an "else" for each "if" condition, otherwise your statements will be prone to error and may show undesired data.
(if {?customPort} <> "" then {ArrivingTrips.port} = {?customPort} else {other.condition}) OR
(if {?airlin} <> "" then {ArrivingTrips.airlines} = {?airlin} else {other.condition}) OR
(if {?hal} <> "" then {ArrivingTrips.hall}={?hal} else {other.condition}) OR
(if (hasvalue({?tim})) then {ArrivingTrips.arrivalTime}={?tim} else {other.condition}) OR
({?d1} = {ArrivingTrips.arrivalDate})
This formula will evaluate if any of the conditions are met. If you need a combination of conditions met, then you will have to join them by using AND.

If Statement that evaluates two conditionals if first is true?

say I wanted the following code:
Sub X
If TextBox1.Text = "Value" then
' Do something
ElseIf TextBox1.Text = "Value1" then
' Also do some other code
End IF
End Sub
How would I do this?
I would like the program to check something first, and if that is true then check something else, and if that is true, also execute that code.
Are you looking for AndAlso?
If TextBox1.Text = "Value" AndAlso TextBox2.Text = "Value1" Then
....
End If
The AndAlso operator performs a logical operation between the two sides of the expression. It evaluates the first condition and if this condition is false it stop further processing (without evaluating the second expression). Only if both conditions are true the code inside the if is executed. This behavior is called short-circuiting evaluation
However, the code in your question cannot be evaluated as true in both conditions for the same TextBox1
If condition1
then
if condition2
then
// do something
end if
end if
If the example in your code is valid, equals value1 then equals value2, do you mean you want if either, because it cannot be both equal?
In this case you can use OR.
Instead of else-if, do:
If TextBox1.Text = "Value" then
' Do something
end if
If TextBox1.Text = "Value1" then
' Also do some other code
End IF
or:
If TextBox1.Text = "Value" then
' Do something
If TextBox1.Text = "Value1" then
' Also do some other code
End IF
end if
Depending on whether you want to execute B only if A is also true.

If...Else... Statement Issue

I have the following code:
Dim UserID As String = "James"
Dim AccNo As String = "S10001"
Dim err As Boolean = False
If Left(AccNo , 1) = "S" Then
If UserID <> "Alex" Or UserID <> "James" Then
err = True
End If
End If
Why I always get the result err=True? Whats wrong with my code?
I think you maybe wanted And rather than Or, because trivially, a UserID will always not be equal to at least one of two distinctly different values, and so one or other of the comparisons will always be true:
If Left(AccNo , 1) = "S" Then
If UserID <> "Alex" And UserID <> "James" Then
err = True
End If
End If
This is because you are doing two 'not' or 'other than' statements which must always be true, because whatever value you test the other is always true.
Because while UserID is equal to 'James' it is also not equals to 'Alex' hence the condition satisfies and changes the variable to true.
That's the right result.
1st condition: Left(AccNo , 1) = "S" is true, so it enters the if block
2nd condition: (UserID <> "Alex" Or UserID <> "James") -> If either of the two result is true, you code will execute the If block.
If you don't want to enter the condition, just change the OR into AND, meaning that the User should NOT be James NOR should he be Alex
I guess you want to express something like "The user id is neither Alex nor James". This in logic would be not (UserID is Alex or UserId is James). As not (x or y) is the same as not x and not y, this equals UserID is not Alex and UserId is not James, or in VB syntax:
UserID <> "Alex" And UserID <> "James"
As a sidenote, you can make your code look a bit nicer, if you use something like this:
If Not {"A", "B", "C"}.Contains("D") Then
' do something
End If
This replaces
If "D" <> "A" AndAlso "D" <> "B" AndAlso "D" <> "C" Then
' do something
End If
Also - as you might have noticed - VB also knows "AndAlso" and "OrElse" which should in most situations used to replace "And" and "Or". Check this for an explanation: AndAlso vs And

Boolean Trouble with If statement VBA Access

I am confused on the conditional operators of VBA. It probably has something to do with weird type casting, but I am stuck and need some help -
query = "SELECT * FROM [records_table] " & _
"WHERE [po_number] = " & Chr(39) & po_number & Chr(39) & ";"
Set rec_set = data_base.OpenRecordset(query)
MsgBox rec_set.Fields("po_ack")
If rec_set.Fields("po_ack") <> Null Then _
po_ack = True
If rec_set.Fields("po_ack") = Null Then _
po_ack = False
Now, this is supposed to return true, but it returns false? When I msgbox the record set value, I get "11/12/2012", but when I msgbox the return of the function, it is false?
po_ack stores a Date value, and the value of this particular one is "11/12/2012", so why when I check it against null, it says that "11/12/2012" == null?
Thanks in advance for any help!
Null is never equal to anything, not even another Null.
Null is never not equal to anything, not even another Null.
Use the IsNull() function to check whether something is Null.
If IsNull(rec_set!po_ack) = False Then
po_ack = True
Else
po_ack = False
End If
See if the IsDate() function could be useful for what you want to do. Here are some sample expressions copied from the Immediate window.
? IsDate(Null)
False
? IsDate(Now())
True
? IsDate("2012-11-12")
True
? IsDate("No way, dood")
False
Sir I think you should take a look at the links listed below. There is a huge difference between NULL, Nothing and a blank value in VB. Null is something unknown, so it is not equal to 1, TRUE, "" or anything else. Never compare a variable with null because it results in incorrect logic. Check if you are confusing these types.
http://www.techrepublic.com/article/learn-the-differences-between-is-null-and-isnull-in-access/5034252
http://www.everythingaccess.com/tutorials.asp?ID=Common-Errors-with-Null
http://www.tek-tips.com/faqs.cfm?fid=3710