[Tutor] Python help

Dennis Lee Bieber wlfraed at ix.netcom.com
Sun Sep 5 14:31:14 EDT 2021


On Sat, 4 Sep 2021 10:16:16 -0700, Sarah Flores <flores.sarahlu at gmail.com>
declaimed the following:

>Hello!
>I have never posted to python.org before.
>I am working on an assignment for a class that is meeting virtually and I
>am looking for some feedback before I have to turn this in Sunday night
>(9/5, midnight pacific time)

	So my comments are probably going to come in too late to be of any use
<G>

	Note: This is a new query, and should have been created as such -- not
attached as a reply to some other unrelated thread.

>Here is the assignment and the code I wrote.
>Can you let me know if I'm totally off base?  For context, I am a total
>noob and this is only my 2nd class in coding. I used mostly the class notes
>ppt slides and random youtube videos to figure this out. It is worth like
>half of the grade for the class so far.)
>
	Have you explored the Python Tutorial...
https://docs.python.org/3/tutorial/

>> * Q1) A non-governmental organization needs a program to calculate the
>> amount of financial assistance for needy families. The formula is as
>> follows: *(300 points)
>> • 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.
>> * Implement a function for this computation. Write a program that asks for
>> the household income and number of children for each applicant, printing
>> the amount returned by your function. Use –1 as a sentinel value for the
>> input.  *
>>
>>
>Code is attached as a txt file.

	You got lucky -- normally the Python forums strip attachments, but it
looks like plain text is the exception (normal extension for Python is .py
-- which may have gotten stripped even if the contents are plain text).

	The common convention for programs is to use

if __name__ = "__main__":
	main()

The intent is that, if someone else imports the file to use functions
defined in it, it doesn't run the main() logic. main() will only be run if
this is the file being executed directly. A corollary of this is that there
should not be statements that actually implement logic at the top level. 

	That said...

	You are asking for the income level outside of main() (and not passing
it into the main routine), but you ask for the number of children buried
deep within the function that computes the aid to be provided.

	The financialAid function, ideally, would be pure -- it would not ask
the user for input, nor would it print any output itself. The income and
#children values would be passed into the function, and the function would
return the results of its computation to the caller (this gets back to my
opening comments -- one could import the file, and invoke the function
themselves, having obtained inputs from somewhere else).

	So -- in my view...

	The bottom of the file would have the above invocation of main() (no
arguments/parameters).

	main() would contain the loop structure, asking the user for income,
AND for the number of children. main() would then invoke
		result = financialAid(income, children)

	financialAid() would do the computations, and return the result of the
computation.

	Note: a possibly advanced feature of Python is that "return" can return
anything -- including a tuple or list -- so you could return a tuple
containing a status along with computed values. This probably constitutes
something a bit more advanced than the class is expecting

	return ("INELIGIBLE", 0, 0)
or
	return("ELIGIBLE", TIERx_BENEFIT, computed_total_aid)

	Then, back in main(), you would test the first part of result:

		if result[0] == "INELIGIBLE":
			print(	your ineligible statement	)
		elif result[0] == "ELIGIBLE":
			print(	eligibility statement with result[1] for tier	)
			print(	Total aid statement with result[2]	)
		else:
			print("*** PROGRAMMING ERROR: expected ELIGIBLE or INELIGIBLE,
got %s" % result[0]


	Another advanced algorithm would be to put the tier limits into a list,
and have financialAid loop over the limits, rather than have all those IF
statements for each level (consider how many changes you'd have to make if
this agency suddenly created a fourth tier). This is really going beyond
your class -- using tuple unpacking in the for loop, break, etc.

# (income limit for tier, minimum children for tier, benefit for tier)
LIMITS = [	(20000, 1, 2000),
			(30000, 2, 1500),
			(40000, 3, 1000)	]
	


def financialAid(income, numchildren):
	eligibilty = False
	tier = 0
	aid = 0
	for (il, mc, bc) in LIMITS:
		if income < il and numchildren >= mc:
			eligibility = True
			tier = bc
			aid = bc * children
			break
	return (eligibility, tier, aid)

	Yes, that is all that is left when structuring the tiers/limits in a
list, and deferring all I/O to the calling function.

	The order of the limits means you don't have to do range checking. If
the income is less than the first limit and they have the required number
of children, you compute the aid, set the tier and eligibility values, and
exit the loop. Otherwise either they didn't have the required number of
children (have none, for the first case), or the income was too high -- go
to second case. The second case wants even more children, so if the first
case failed for lack of children, so will all others. You don't need to
test for income > tier1 limit because that was checked in the first case,
and (if they had children) failed the income limit to get to the second
case.

	Adding a fourth tier just requires inserting one tuple into the list,
having the next income limit, minchildren limit, and the benefit per child.
No changes to blocks of IF statements.



-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/



More information about the Tutor mailing list