Nested conditions can get pretty deep very quickly. That makes reading the code difficult and programmers tend to avoid having many nested conditions (and having too much-nested code in general). Python provides a utility elif that helps in avoiding else and then if statements:
if name == 'Alice':
print('Hey there!')
else:
if name == 'Bob':
print('How are you doing?')
else:
if name == 'Anna':
print('Hello, Anna')
else:
print('Hi!')
if name == 'Alice':
print('Hey there!')
elif name == 'Bob':
print('How are you doing?')
elif name == 'Anna':
print('Hello, Anna')
else:
print('Hi!')
These two programs do exactly the same. They print personalized greeting messages and contain many if/else statements. Yet, the first one has many layers of nested if/else blocks, while the second uses elif statements which stands for else if.
Challenge
Having a grade which is a number from 0 to 100, we would like to know what would that grade correspond to in the US Letter grading system (A, B, C, D, and F).
Note: Do not use only if statements with range checks. Use elif when checking for another condition.