+ 1

Why cant elif be used at end

num = 7 if num == 5: print("Number is 5") elif num == 11: print("Number is 11") elif num == 7: print("Number is 7") else: print("Number isn't 5, 11 or 7") Give no errors num = 7 if num == 5: print("Number is 5") elif num == 11: print("Number is 11") elif num == 7: print("Number is 7") elif: print("Number isn't 5, 11 or 7") Gives symtex error why?

2nd Dec 2018, 8:53 PM
Jordan Autrey
Jordan Autrey - avatar
6 Antworten
+ 1
A condition is a logical expression such as 4 < 6 which can only result in 2 values: true or false. elif means “else if” which kind of explains why you need a condition.
2nd Dec 2018, 9:12 PM
TurtleShell
TurtleShell - avatar
+ 2
elif statements require a condition hence else IF
2nd Dec 2018, 8:59 PM
TurtleShell
TurtleShell - avatar
+ 1
I'm sorry I'm like a complete noob, what does "condition hence" mean. Can you do this as like you talkin to a 5 year old trying to learn programming
2nd Dec 2018, 9:07 PM
Jordan Autrey
Jordan Autrey - avatar
+ 1
Thank TurtleShell and HonFu for taking the time to explain the best example of Else and elif and why I can't use the elif: at the else like else:
2nd Dec 2018, 9:45 PM
Jordan Autrey
Jordan Autrey - avatar
0
Let's say you let a user input a number between 1 and 10. If x==1, then one thing is supposed to happen. elif x==2, another thing happens. And in ANY OTHER CASE (x==3, or 7 or 'apple') another thing happens. else defines this 'any other case'. It is the most general statement, so it logically comes last (if it comes).
2nd Dec 2018, 9:25 PM
HonFu
HonFu - avatar
0
You can use elif at the end - but only when there is no else.
2nd Dec 2018, 9:47 PM
HonFu
HonFu - avatar