fb pixel

Introduction to Python

  • Profound Academy

    • Status
      • 1
        Input and Output
      • 2
        Variables and Integer Arithmetic
      • 3
        Conditional Operators
      • 4
        Advanced Conditions - Nesting
      • 5
        Types and Variables
      • 6
        Strings
      • 7
        Lists
      • 8
        For Loops
      • 9
        While Loops
      • 10
        continue, break, while...else
      • 11
        String and List methods
      • 12
        Nested Loops
      • 13
        List Comprehension
      • 14
        Tuples and Sets
      • 15
        dict
      • 16
        Functions
      • 17
        Functions 2
      • 18
        Lambda and higher-order functions
      • 19
        Files

  • elif

    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.
    Numeric grade
    Letter grade
    90-100
    A
    80-89
    B
    70-79
    C
    60-69
    D
    0-59
    F
    Write a program that would do the conversion.
    Input
    Output
    81
    B
    100
    A
     
    To check your solution you need to sign in
    Sign in to continue