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:'))
Answer from Frames Catherine White on Stack Overflowpython - What is the correct syntax for 'else if'? - Stack Overflow
python "else" without if
How can I make sense of the `else` clause of Python loops? - Stack Overflow
Benefits of using for else syntax
Videos
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:'))
Do you mean elif?
Hi, I've found this code
num = 407
if num == 1:
print(num, "is not a prime number")
elif num > 1:
# check for factors
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number")
# if input number is less than
# or equal to 1, it is not prime
else:
print(num,"is not a prime number")The "else" outside the for loop doesn't match any "if" before it, but the code still works. How?
An if statement runs its else clause if its condition evaluates to false.
Identically, a while loop runs the else clause if its condition evaluates to false.
This rule matches the behavior you described:
- In normal execution, the while loop repeatedly runs until the condition evaluates to false, and therefore naturally exiting the loop runs the else clause.
- When you execute a
breakstatement, you exit out of the loop without evaluating the condition, so the condition cannot evaluate to false and you never run the else clause. - When you execute a
continuestatement, you evaluate the condition again, and do exactly what you normally would at the beginning of a loop iteration. So, if the condition is true, you keep looping, but if it is false you run the else clause. - Other methods of exiting the loop, such as
return, do not evaluate the condition and therefore do not run the else clause.
for loops behave the same way. Just consider the condition as true if the iterator has more elements, or false otherwise.
Better to think of it this way: The else block will always be executed if everything goes right in the preceding for block such that it reaches exhaustion.
Right in this context will mean no exception, no break, no return. Any statement that hijacks control from for will cause the else block to be bypassed.
A common use case is found when searching for an item in an iterable, for which the search is either called off when the item is found or a "not found" flag is raised/printed via the following else block:
Copyfor items in basket:
if isinstance(item, Egg):
break
else:
print("No eggs in basket")
A continue does not hijack control from for, so control will proceed to the else after the for is exhausted.