What Is the Correct Syntax for 'Else If'

What is the correct syntax for 'else if'?

In python "else if" is spelled "elif".

Also, you need a colon after the elif and the else.

Simple answer to a simple question. I had the same problem, when I first started (in the last couple of weeks).

So your code should read:

def function(a):
if a == '1':
print('1a')
elif a == '2':
print('2a')
else:
print('3a')

function(input('input:'))

Putting an if-elif-else statement on one line?

No, it's not possible (at least not with arbitrary statements), nor is it desirable. Fitting everything on one line would most likely violate PEP-8 where it is mandated that lines should not exceed 80 characters in length.

It's also against the Zen of Python: "Readability counts". (Type import this at the Python prompt to read the whole thing).

You can use a ternary expression in Python, but only for expressions, not for statements:

>>> a = "Hello" if foo() else "Goodbye"

Edit:

Your revised question now shows that the three statements are identical except for the value being assigned. In that case, a chained ternary operator does work, but I still think that it's less readable:

>>> i=100
>>> a = 1 if i<100 else 2 if i>100 else 0
>>> a
0
>>> i=101
>>> a = 1 if i<100 else 2 if i>100 else 0
>>> a
2
>>> i=99
>>> a = 1 if i<100 else 2 if i>100 else 0
>>> a
1

How can I use a conditional expression (expression with if and else) in a list comprehension?

x if y else z is the syntax for the expression you're returning for each element. Thus you need:

[ x if x%2 else x*100 for x in range(1, 10) ]

The confusion arises from the fact you're using a filter in the first example, but not in the second. In the second example you're only mapping each value to another, using a ternary-operator expression.

With a filter, you need:

[ EXP for x in seq if COND ]

Without a filter you need:

[ EXP for x in seq ]

and in your second example, the expression is a "complex" one, which happens to involve an if-else.

Correct syntax of @if @else ladder in Laravel blade

You have the conditions upside down basically. At the moment if they have any notifications at all the first condition is true so it never will reach the elseif parts.

@if(auth()->user()->notifications > 15)
danger
@elseif(auth()->user()->notifications > 7)
warning
@elseif(auth()->user()->notifications != 0)
success
@endif

What is the correct syntax for this 'OR' and 'AND' in this 'IF' statement?

The two statements are different. Yes, they are both valid statements, syntactically, but logically they differ. Since the && operator has a higher precedence than the || in javscript,
the resulting logic will evaluate as follows in statement 2:

1) (type == 'BRS') && (type2 == 'BOS')
2) (type == 'BOS') || (type == 'BPS') || (result of 1) || (type2 == 'BPS') || (type2 == 'BRS')

While in statement 1:

1) (type == 'BOS'|| type == 'BPS'|| type == 'BRS')
2) (type2 == 'BOS'|| type2 == 'BPS'|| type2 == 'BRS')
3) (result of 1) && (result of 2)

swift correct syntax if else while etc

The issue is that once you exit the rectangle, you lose the ability to change the position of the paddle. Because of that, every subsequent check of backgroundRect.contains(paddleRect) will return false, forever.

What you should do instead is always compute the potential position, and either use it, or discard it:

let potentialPosition = CGPoint(
x: paddle.position.x + (touchLocation.x - previousLocation.x),
y: paddle.position.y + (touchLocation.y - previousLocation.y)
)

// This line is approximated, I don't know the exact relation between
// `paddleRect` and `paddle`, but you get the idea
if backgroundRect.contains(potentialPosition) {
paddle.position = potentialPosition
} else {
print ("paddleRect not contained by backgroundRect")
}


Related Topics



Leave a reply



Submit