The big if


#1

I use following coding for this after getting a little help from here , and its working fine.

def grade_converter(grade):
if grade >= 90:
return "A"
elif (grade >79) or (grade ==90):
return "B"
elif (grade >69) or (grade ==79):
return "C"
elif (grade >64) or (grade ==69):
return "D"
else:
return “F”


The big if (I'm stuck on that)
#2

@akashgupta2492, If you use fall through, you don’t have to use to use AND. This point has already been made by mtf


#3

Can you explain it, I didn’t understand what you said. I will happy if you help me to make better codes.


#4

you use the or operator, i guess i was a bit hasty.

when grade=90, the if condition is true. The rest of the condition aren’t checked, it ends the moment a condition becomes true.

when grade=80, the first elif condition is true. So we don’t need to check if value is less then 90, because if it wasn’t, the if condition would have been true, and the elif would never have ran.

if you have if followed by elif and else, the moment a condition is true, the rest of the conditions aren’t checked

your or (grade ==90) is pointless, if the grade was 90, the if condition would be true


#5

use indentation for better program (i.e white spaces below if condition)


#6

he did? Its just not showing because in order for indent to show up on the forum, because you need to use format.

Also, the python program won’t run if it wasn’t properly indented.


#7

Thank you, Sir, for your kind help, Now I understand what you want to teach me. Great and thank you again. I will try it another way and also check it properly.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.