Names that say what they mean
Code is read far more often than it is written, and names carry most of its meaning. A good name says what a value is or what a function does, in the words of the problem, so the reader does not have to decode it.
def calc(l, t):
r = []
for x in l:
if x[1] >= t:
r.append(x[0])
return r
def passing_names(results, pass_mark):
return [name for name, score in results
if score >= pass_mark]
data = [("Ada", 81), ("Bo", 47)]
print(calc(data, 50))
print(passing_names(data, 50))Both functions do the same thing. Only the second can be understood without running it.
- Functions do something, so name them after the action or the result:
send_invoice,total_price. - Booleans read as yes or no questions:
is_active,has_stock. - Collections are plural:
users,prices_by_item. - Avoid one-letter names and abbreviations only you know.
Your task
Rename f to top_scorers(scores, count) and give every local a real name: it returns the names of the count highest values in the dict scores, highest first, ties in alphabetical order. Rename chk to is_passing(score, pass_mark=50), returning a bool.
The explanation opens here once every check passes.