Review, suggestion etc?
Joe Pfeiffer
pfeiffer at cs.nmsu.edu
Thu Dec 17 20:17:32 EST 2020
Bischoop <Bischoop at vimart.net> writes:
> On 2020-12-17, Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
>>>
>>
>> The main concern is that you are using a RECURSIVE call. It is much
>> better for such input checking to use an ITERATIVE (loop) scheme.
>>
>> def marriage():
>> #loop forever
>> while True:
>> #get response from user
>> maritals = input("Married: Yes/No ?").title()
>> #if response is good, exit (break) the loop
>> if maritals in ["Yes", "No"]: break
>> #otherwise display error message and repeat loop
>> print("Try again. Please respond with Yes or No")
>> #return valid response
>> return maritals
>>
>>
> It makes sense for me now, better logic used here than mine.
> I've never met that way used in * if maritals in ['Yes', No']: * ,
> it makes code elegant.
> So it's not good to use calling function itself (recursive call), I get it now.
Recursion has very limited application, but where it's the right tool
it's invaluable (top-down parsers, some graph algorithms...). We teach
it primarily because by the time a student has a good handle on how to
write a recursive function they understand functions in general really well.
More information about the Python-list
mailing list