+ 1
Where's the wrong?
num = 7 if num == 8: print("Hasilnya Salah") else: if num == 7: print("Hasilnya Benar") print("Program Selesai")
12 Answers
+ 11
''' @Duta : Yes, you can use else: if in your code. Your syntax was correct but you didn't indent it well. Example : '''
num = 7
if num == 8:
print("Hasilnya Salah")
else: # no indention needed
if num == 7:
print("Hasilnya Benar")
print("Program Selesai")
+ 10
num = 7
if num == 8:
print("Hasilnya Salah")
elif num == 7:
print("Hasilnya Benar")
print("Program Selesai")
+ 10
@Duta : It's not giving any type of error. Just copy paste my previous answer to the python's code playground ...
+ 7
elif=else+if+indentation saver.
What you are doing now is a nested if-else loop
A more efficient one (Dayve's) is an if-elif-else loop
+ 7
True
+ 2
thanks, but, else and elif is same right? so, what it's was wrong? because my python is 3.6? or what? i was new begin learn python.
+ 2
@Duta else and elif aren't the same.
When you use elif is because you want to verify a new specific condition but when you use else then you are agree with do some instructions for all other posibilities which before weren't true. Sometimes into an else or first if you can mix these conditional commands
Your example:
num = 7
if num == 8:
print("Hasilnya Salah")
else:
if num == 7:
print("Hasilnya Benar")
print("Program Selesai")
Mainly, when you use "else" you are accepting num can be greater or less but not equal, so it could be any possibility diferent of 8.
elif num == 7:
print("Hasilnya Benar")
print("Program Selesai")
Now, if you don't use else and use elif, you are trying to verify only if num==7 but all other possibilities are out of your code.
You can program as you want or obviously, following your needs but basic sintax is
If condition1 then do instructions1
elif condition2 then do instructions2
elif condition3 then do instructions3
else then do instructions4
With that sintax you are covering some specific conditions (3 exactly) and of course with "else" all other possible values. This are usual for to do a more complete program.
Most of the time you use only IF and ELSE but if you want to check other conditions, you can use elif or even nested IF. When you are programming sometimes you will use other struct's conditions.
+ 2
thanks for your answer all
+ 1
still error bro
+ 1
I hope that you are better now about sintax and using of if
+ 1
If not, you can share your code from your profile to help you ;-)
+ 1
oke bro, thank you for teached me.