In: Computer Science
Python code
Financial Assistance : A non-governmental organization needs a program to calculate the amount of financial assistance for needy families.
The formula is as follows:
• If the annual household income is between $30,000 and $40,000 and the household has at least three children, the amount is $1,000 per child.
• If the annual household income is between $20,000 and $30,000 and the household has at least two children, the amount is $1,500 per child.
• If the annual household income is less than $20,000, the amount is $2,000 per child. Write a program that asks for the house-hold income and number of children for each applicant, printing the amount returned by your function.
Input function Specs :
1. def get_income() This function requests an annual household income from the user and continues to ask for the income value unntil the user gets it right. This function should not accept any arguments.
2. def get_number() This function requests a number of children and continues to ask for the number until the user gets it right. This function should not accept any arguments.
3. def compute_assistance(income, number) This function accepts both the annual household income and the number of children as parameters and return the amount of financial assistance.
Input Errors Whenever the user makes an input error, keep at them until they get it right. Do not return from an input function until you have acquired a legal value, even if it takes years.
I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments (read them for better understanding).
Images of the Code:
Note: If the below code is missing indentation please refer code Images
Typed Code:
# a function called get_income
# no input parameters
def get_income():
# A infinite while loop
# this loop run infinitely until user enter a valid input
while(True):
# reading annual income from user
income=int(input("Enter your annual household income:"))
# if income is a non negative value or valid input
if(income >= 0):
# breaking the loop
break
else:
# printing the Invalid message
print("Invalid Input. Try Again")
# returning the income
return income
# a function called get_number
# no input parameters
def get_number():
# A infinite while loop
# this loop run infinitely until user enter a valid input
while(True):
# reading number of children's from user
number = int(input("Enter number of children's in your family
:"))
# if children's is a non negative number or valid input
if(number >= 0):
# breaking the loop
break;
else:
# printing the Invalid message
# Asking input again
print("Invalid Input. Try Again")
# returning the number of children's
return number
# a function called compute_assistance
# with input parameters income and number
def compute_assistance(income, number):
# a variable to store financial assistance amount
financial_assistance=0;
# if income is between $30000 and $40000
# and the household has at least three children(greater than or
equal to)
if(30000 <= income < 40000 and number>= 3):
# calculating financial_assistance $1000 per child
financial_assistance = number*1000
# if income is between $20000 and $30000
# and the household has at least two children(greater than or equal
to)
elif(20000 <= income < 30000 and number>= 2):
# calculating financial_assistance $1500 per child
financial_assistance = number*1500
# if household income is less than $20000
elif(income<20000):
# calculating financial_assistance $2000 per child
financial_assistance = number*2000
# returning the financial_assistance
return financial_assistance;
# calling the function get_income
income=get_income()
# calling the function get_number
number=get_number()
# calling the function financial_assistance
financial_assistance=compute_assistance(income,number)
# printing the returned amount
print("The amount of financial assistance is
$",financial_assistance)
#code ended here
Output:
If You Have Any Doubts. Please Ask Using Comments.
Have A Great Day!