Skip to content

Instantly share code, notes, and snippets.

@Mintonne
Last active February 18, 2025 15:56
Show Gist options
  • Save Mintonne/927af8ec4bf12bb9e4aab08a9c701d25 to your computer and use it in GitHub Desktop.
Save Mintonne/927af8ec4bf12bb9e4aab08a9c701d25 to your computer and use it in GitHub Desktop.
Treasury Bills Calculator
def calculate_treasury_bill_return():
"""
Calculates the return on a Treasury bill.
Fixed tax rate (if applicable), 365 days/year, and specific maturity options.
"""
# Get inputs from the user
while True:
try:
face_value = float(input("Enter the Face Value (e.g., 5000000): "))
if face_value <= 0:
print("Face Value must be positive.")
continue
break
except ValueError:
print("Invalid input. Please enter a numeric value for Face Value.")
while True:
try:
interest_rate_percent = float(input("Enter the annual Interest Rate (as a percentage, e.g., 5.5): "))
break
except ValueError:
print("Invalid input. Please enter a numeric value for Interest Rate.")
while True:
try:
days_to_maturity = int(input("Enter the Days to Maturity (91, 182, or 364): "))
if days_to_maturity not in [91, 182, 364]:
print("Days to Maturity must be 91, 182, or 364.")
continue
break
except ValueError:
print("Invalid input. Please enter an integer value for Days to Maturity.")
while True:
tax_exempt_input = input("Is the investment tax-exempt? (yes/no): ").lower()
if tax_exempt_input in ('yes', 'no', 'y', 'n'):
tax_exempt = tax_exempt_input in ('yes', 'y')
break
else:
print("Invalid input. Please enter 'yes' or 'no'.")
# Fixed values
days_in_year = 365
tax_rate = 0.15 if not tax_exempt else 0.0
# Calculations
p = 100 * (1 / (1 + (interest_rate_percent / 100) * (days_to_maturity / days_in_year)))
investment_amount = (face_value / 100) * p
gross_return = face_value - investment_amount
withholding_tax = gross_return * tax_rate
net_return = gross_return - withholding_tax
total_paid = investment_amount + withholding_tax
# Output
print("\n--- Investment Summary ---")
print(f"Total Investment Amount: Ksh {investment_amount:,.2f}")
print(f"Price per Ksh 100 (P): Ksh {p:,.2f}")
print(f"Gross Return: Ksh {gross_return:,.2f}")
if not tax_exempt:
print(f"Withholding Tax: Ksh {withholding_tax:,.2f}")
print(f"Net Return: Ksh {net_return:,.2f}")
print(f"Total amount (Investment + Tax): Ksh {total_paid:,.2f}")
if __name__ == "__main__":
calculate_treasury_bill_return()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment