Python Forum
Small to medium Scripts/Snippets - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: Code sharing (https://python-forum.io/forum-5.html)
+--- Thread: Small to medium Scripts/Snippets (/thread-105.html)



Small to medium Scripts/Snippets - snippsat - Sep-19-2016

As title say this will be a sticky thread where we all can post small to medium Scripts/Snippets.
Index over scripts in this thread.

  1. Convert grades problem using Bisect



RE: Small to medium Scripts/Snippets - snippsat - Sep-20-2016

Old convert grades problem without if,elif.., using Bisect.
import bisect

def grade(total):
    return grades[bisect.bisect(breakpoints, total)]

if __name__ == '__main__':
    # F is 30 or lower,45-60 is E  ect...
    grades = "FEDCBA"
    breakpoints = [30, 45, 60, 75, 85]

    # Students
    heroes = dict(Superman=90, Hulk=25, Batman=50)
    for k,v in heroes.items():
        print('{} had score of: {} which is grad: {}'.format(k, v, grade(v)))

'''Output-->
Hulk had score of: 25 which is grad: F
Batman had score of: 50 which is grad: D
Superman had score of: 90 which is grad: A
'''