Skip to content

Instantly share code, notes, and snippets.

@kangmasjuqi
Last active August 12, 2018 04:56
Show Gist options
  • Save kangmasjuqi/6822c4ffd26b5a6d15fb434da686d046 to your computer and use it in GitHub Desktop.
Save kangmasjuqi/6822c4ffd26b5a6d15fb434da686d046 to your computer and use it in GitHub Desktop.
allowance - simulate allowance calculation based on the salary, the number of childrens and their age
##
## Bismillaahirrahmaanirrahiim..
## Allahumma sholli 'alaa sayyidinaa Muhammad..
##
## allowance.py
## created by marjuqi r.
## august 2018
##
## run using : python allowance.py
##
## this simple program is designed to simulate allowance calculation
## based on the salary, the number of childrens and their age
##
from __future__ import unicode_literals
from prompt_toolkit import prompt
def sanitize_input(inputstr):
sanitized = inputstr
badstrings = [
' ',
';',
'$',
'&&',
'../',
'<',
'>',
'%3C',
'%3E',
'\'',
'--',
'\x00',
'`',
'(',
')',
'file://',
'input://'
]
for badstr in badstrings:
if badstr in sanitized:
sanitized = sanitized.replace(badstr, '')
return sanitized
def get_allowance_percentage_based_on_age(age):
if age < 10:
return 0.01
elif age < 15:
return 0.02
elif age < 20:
return 0.03
else :
return 0.03
def check_valid_integer(input):
try:
val = int(input)
return True
except ValueError:
print("That's not a numeric! Please input a valid numeric")
return False
def ui(command):
return sanitize_input(prompt(command))
if __name__ == '__main__':
print("====================")
print ("salam,\nthis simple program is designed to simulate allowance calculation \nbased on the salary, the number of childrens and their age")
print("====================")
default_allowance_percentage = 0.03 # constant
n_accomodated_childs = 3 # constant
salary = 0
n_child = 0
allowances= 0
childs = []
is_int = False
while is_int == False:
salary = ui('Input your salary ? ')
is_int = check_valid_integer(salary)
is_int = False
while is_int == False:
n_child = ui('Input your number of child ? ')
is_int = check_valid_integer(n_child)
childs_string = ui('Input your childs age, separated by comma ? ')
childs = map(int, childs_string.split(","))
salary = float(salary)
print('Your salary: ' + str(salary))
print('Your n_child: ' + str(n_child))
print('Your allowances detail: ')
if int(n_child) == 1:
allowances = salary * default_allowance_percentage
print('- child ' + str(childs[0]) + ' yo : ' + str(default_allowance_percentage) + ' * ' + str(salary) + ' = '+ str(allowances))
else :
ii = 0
childs.sort(reverse=True)
while(ii < n_child and ii < n_accomodated_childs):
percentage = get_allowance_percentage_based_on_age(childs[ii])
allowance = percentage * salary
allowances +=allowance
print('- child ' + str(childs[ii]) + ' yo : ' + str(percentage) + ' * ' + str(salary) + ' = '+ str(allowance))
ii=ii+1
print('Your total THP: ' + str(salary + allowances))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment