+ 1

Where's the wrong?

num = 7 if num == 8: print("Hasilnya Salah") else: if num == 7: print("Hasilnya Benar") print("Program Selesai")

6th Aug 2017, 6:20 AM
Fariz Duta Nugraha
Fariz Duta Nugraha - avatar
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")
6th Aug 2017, 6:55 AM
Dev
Dev - avatar
+ 10
num = 7 if num == 8: print("Hasilnya Salah") elif num == 7: print("Hasilnya Benar") print("Program Selesai")
6th Aug 2017, 6:35 AM
Dev
Dev - avatar
+ 10
@Duta : It's not giving any type of error. Just copy paste my previous answer to the python's code playground ...
6th Aug 2017, 7:26 AM
Dev
Dev - avatar
+ 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
6th Aug 2017, 6:59 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 7
True
6th Aug 2017, 8:07 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 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.
6th Aug 2017, 6:46 AM
Fariz Duta Nugraha
Fariz Duta Nugraha - avatar
+ 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.
6th Aug 2017, 7:53 AM
Daniel
Daniel - avatar
+ 2
thanks for your answer all
6th Aug 2017, 8:01 AM
Fariz Duta Nugraha
Fariz Duta Nugraha - avatar
+ 1
still error bro
6th Aug 2017, 7:23 AM
Fariz Duta Nugraha
Fariz Duta Nugraha - avatar
+ 1
I hope that you are better now about sintax and using of if
6th Aug 2017, 8:03 AM
Daniel
Daniel - avatar
+ 1
If not, you can share your code from your profile to help you ;-)
6th Aug 2017, 8:04 AM
Daniel
Daniel - avatar
+ 1
oke bro, thank you for teached me.
6th Aug 2017, 8:07 AM
Fariz Duta Nugraha
Fariz Duta Nugraha - avatar