+ 1

what's wrong with this code?

print ("Program menghitung nilai akhir\n") nama = input ("Masukkan nama : ") tugas = input ("Masukan nilai tugas : ") uts = input ("Masukkan nilai UTS : ") uas = input ("Masukkan nilai UAS : ") print ("\n") na = 0.25 * float (tugas) + 0.35 * float (uts) + 0.40 * float (uas) print ("Nama = ", nama) print ("Nilai akhir = ", na) if na >= 75 : print ("Grade = A") if na >= 60 : print ("Grade = B") if na >= 45 : print ("Grade = C") if na <=45 : print ("Grade = D")

16th Mar 2022, 6:05 AM
Green Wood
Green Wood - avatar
6 odpowiedzi
+ 3
Indentation error. After the first if statement, all the rest are nested under it. Unindent them to match the first one, but leave the print statements as they are. Then make those if statements into elif statements.
16th Mar 2022, 6:12 AM
Brian
Brian - avatar
+ 2
Sure. Like this: if na >= 75 : print ("Grade = A") elif na >= 60 : print ("Grade = B") elif na >= 45 : print ("Grade = C") elif na <=45 : print ("Grade = D")
16th Mar 2022, 6:23 AM
Brian
Brian - avatar
+ 2
oh, it's work. thank you
16th Mar 2022, 6:26 AM
Green Wood
Green Wood - avatar
+ 1
One more thing... I just noticed also that the last condition should be changed from <=45 to <45, since the condition above it already checked for =45. Furthermore, since the last condition is meant to account for all the rest of the marks, you may as well make it just plain else. else : print ("Grade = D")
16th Mar 2022, 6:31 AM
Brian
Brian - avatar
+ 1
okay i understand, thx again
16th Mar 2022, 6:36 AM
Green Wood
Green Wood - avatar
0
so i need elif after the first if? can u give me example?
16th Mar 2022, 6:20 AM
Green Wood
Green Wood - avatar