Software engineering starts with small pieces. A function should do one job, take named inputs, and return a value. print belongs at the edge — in main(), not inside every helper.
Goal
Split a kiosk total into helpers that return values, then print once.
Return, then print
TAX = 0.16
def line_total(units, price):
return round(units * price * (1 + TAX), 2)
def format_kes(amount):
return f"KES {amount:.2f}"
def main():
total = line_total(3, 10.5)
print("Nairobi")
print(format_kes(total))
if __name__ == "__main__":
main()line_total does not print. Tests can call it without scraping the console.
Names that say what happened
def city_label(city):
return str(city or "").strip().title()
print(city_label(" mombasa "))
print(city_label("kisumu"))city_label is better than fn or process. Use snake_case for functions and variables.
Too much in one place
def report(units, price, city):
tax = 0.16
total = round(units * price * (1 + tax), 2)
label = str(city).strip().title()
print(label)
print(total)
return total
print("avoid this shape — helpers mixed with print")
report(2, 10.5, "nairobi")That function computes, formats, and prints. Split it.
Pitfall
A function that only prints is hard to test. Return the value; print in main().