Python Forum
Small to medium Scripts/Snippets
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Small to medium Scripts/Snippets
#1
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
#2
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
'''


Possibly Related Threads…
Thread Author Replies Views Last Post
  300 free curated Python code snippets steve_shambles 1 44,896 Jul-14-2020, 03:32 PM
Last Post: HarleyQuin
  200 Python code snippets free steve_shambles 6 4,954 Apr-19-2020, 12:38 PM
Last Post: Calli

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020