Can I add an else to a simplified if statement? - if-statement

I'll be honest, I could not think of what to address this problem as.
I was looking on Coffeescript.org to see if there was a nicer way to handle multiple OR's in an if statement
They showed an example of displaying this:
if (pick === 47 || pick === 92 || pick === 13) {
winner = true;
}
List this this:
winner = yes if pick in [47, 92, 13]
My problem is that I can seem to put an else at the end of the if statement when I have it formatted the new way. Are you even allowed to?
Thanks for your time!

You have to use if...then if you want to use an else:
winner = if pick in [47, 92, 13] then yes else no
However, you can still do multi-line ifs:
if pick in [47, 92, 13]
winner = true
else
winner = false
Also, note that the in operator returns a boolean, so you can assign that directly.
winner = pick in [47, 92, 13]
is equivalent to my first example.

Related

Power Query column based on conditionals

Hey guys new to Power BI here and I am having some trouble creating a column based on conditionals in Power Query.
I am trying to do something like if the previous letter was A,B,or C and the current letter is E,F,or G mark True, else False.
My code looks like this so far:
Custom Column =
IF List.Contains({“A”, “B”,”C”}), [PrevLetter]
AND list.Contains({“E”, “F”, ”G”}) , [Letters]
THEN “TRUE”
ELSE “FALSE”
Output would be so something like:
PrevLetter
Letter
CustomColumn
A
F
True
X
M
False
B
E
True
C
F
True
Any help is truly appreciated!
The answer depends on if you are looking for a letter within a word, or if that letter represents the entire text, but for your example, one answer could be
= if Text.PositionOfAny([PrevLetter], {"A", "B","C"})+Text.PositionOfAny([Letter], {"E", "F","G"})=0 then true else false
or if you want to plan around nulls
= try if Text.PositionOfAny([PrevLetter], {"A", "B","C"})+Text.PositionOfAny([Letter], {"E", "F","G"})=0 then true else false otherwise false
or
= Table.AddColumn(Source, "Custom", each if List.ContainsAny({[PrevLetter]},{"A","B","C"}) and List.ContainsAny({[Letter]},{"D","E","F"}) then true else false )

nextflow (groovy) check if item in Channel list

I am struggling to use an if/else on a containsAll() statement. It returns the correct true false value when tested with println(), but when put in an if statement it seems to always evaluate to true -- see below.
def examine_phenotype(pheno){
condition_values = \
Channel
.fromPath(pheno)
.splitCsv(header: true, sep: ',')
.map{ row ->
def condition = row.condition
return condition
}
.toList().view()
println(condition_values.containsAll('control'))
if(condition_values.containsAll('control')){
exit 1, "eval true"
}else{
exit 1, "eval false"
}
}
Console output for two different files, one with 'control' and one without 'control' in the column 'condition', which is the point of the function.
[normal, normal, normal, tumor, tumor, tumor]
DataflowInvocationExpression(value=false)
eval true
[control, control, control, tumor, tumor, tumor]
DataflowInvocationExpression(value=true)
eval true
Using collect() instead of toList() where each item within condition_values is enclosed with single quotes did not resolve the issue either. The clue might be in DataflowInvocationExpression but I am not up to speed on Groovy yet and am not sure how to proceed.
Testing the conditional within the function was not working, but applying filter{} and ifEmpty{} was able to produce the desired check:
ch_phenotype = Channel.empty()
if(pheno_path){
pheno_file = file(pheno_path)
ch_phenotype = examine_phenotype(pheno_file)
ch_phenotype.filter{ it =~/control/ }
.ifEmpty{ exit 1, "no control values in condition column"}
}
def examine_phenotype(pheno){
Channel
.fromPath(pheno)
.splitCsv(header: true, sep: ',')
.map{ row ->
def condition = row.condition
return condition
}
.toList()
}

How do I have a user enter values between 1 and 5, where 1 will return the employee information saved in the list index [0] in python [duplicate]

I am very new to programming in Python, this is something that seems so simple, but I just don't seem to be able to get it right.
I have a list of values.
I want to prompt the user for input.
Then print out the value that's at the corresponding index number in the list.
myList [0, 1, 20, 30, 40]
choice = input()
print (......)
If the user inputs 2, I want to print the the value that is at the index 2 (20). I am unsure of what to put after print.
You can do this by accessing the list using the given input by the user.
The input given by the user I assume would be a String, so we need to use int() to change it to an integer.
print myList[int(choice)]
Additionally, you may want to first check the validity of the input supplied; it cannot be less than 0, or more than the length of the list - 1;
choice = int(choice)
if (choice < 0 || choice >= len(myList))
print('Not Valid')
else
print myList[int(choice)]

How do I call variable into another function from another function

I'm working on a game and am stuck on a part where a key is supposed to unlock the door of a room. Now, after some searching, I've come across that i can't call a variable that exists in one function, into another function: as i have been trying to by setting i.e.key_picked = True under a conditional in the kitchen function. And then, using a conditional in the room function, with the key_pickedin a Boolean expression.
So, how do i work around this?
def kitchen(already_there=False):
if choice02_key == "pick key" or choice02_key == "0" or choice02_key == "key":
print("---------------------------------------------------------")
print "You picked the key. It probably unlocks some door."
key_picked = True
kitchen(already_there=True)
def room01(already_there=False):
if key_pick == True:
print("---------------------------------------------------------")
print "You unlocked the room using the key."
else:
print "This room is locked."
entrance_hall(already_there=True)
You can pass the variable in a parameter. For example:
Define keyPicked in room01.
Call kitchen(already_there, keyPicked) from room01.
Make the assignment you want.
Then, you will have the value you want in keyPicked.
For example, imagine I have a function that add 10 to a number. This will be better to return the value, but it is just to show you how can you do it.
def add_ten(number):
number = number + 10
def main():
number = 5
print('Number is:', number)
add_ten(number)
print('Number is:', number)
Output:
Number is: 5
Number is: 15

Python If statement-differences

I try to learn the If statement forms integrated with For loop type, and i can't understand the differences between those codes because they give the same result:
grade = [100, 97, 73, 56, 78,34]
for i in range(0,len(grade)):
if grade[i]%2 == 0:
grade[i]= grade[i]+2
if grade[i]%3 ==0:
grade[i]= grade[i]+3
if grade[i]%5 ==0:
grade[i]= grade[i]+5
print grade
and this:
grade = [100, 97, 73, 56, 78,34]
for i in range(0,len(grade)):
if grade[i]%2 == 0:
grade[i]= grade[i]+2
if grade[i]%3 ==0:
grade[i]= grade[i]+3
if grade[i]%5 ==0:
grade[i]= grade[i]+5
print grade
When you have if statements one below another it's possible that something can match one OR another.
When you have nested if statements, to go through your condition has to match one AND another.
Consider in your first case: 10. It will pass %2 == 0 and %5 == 0, but not the %3 == 0. In second case it will only pass the first test and won't go to the nested ones.
For instance: 30 will pass all the if statements in both case.
Both code is same but the main difference is first code contains three if condition that executed top to bottom or one by one and second code contains three nested if condition statement that execute if first statement is true
learn more from c-sharpcorner.com