Reputation: 9672
I have a prompt in terminal that is calling itself without being asked to. The behavior in question is if it is fed names like ['artist', 'art', 'dogs'], and the match matches 1 of them, when it reaches elif len(matches) == 1:
, it will ask you to confirm "Is dogs OK?" twice. It always asks twice, then correctly returns 'dogs'
def hostfunc(names):
def _prompt(answer=None):
if answer == None:
answer = gather("Pick from {names}...if you want more than 1, separate like \"name1, name2\"".format(names=names))
_prompt(answer=answer)
matches = check_for_answer_matches(names, answer)
matches_string = process_matches(matches)
if len(matches) == 0:
answer = gather("That string didn't match any of {names}, please try again".format(names=names))
_prompt(answer=answer)
elif len(matches) == 1:
answer = gather("Is {matches_string} OK? ('y' or 'n' to restart)".format(matches_string=matches_string))
if answer == 'y':
return matches
elif answer == 'n':
return _prompt()
return matches
elif len(matches) > 1:
answer = gather("That string matched {matches_string}, is that OK? ('y' or 'n' to restart)".format(matches_string=matches_string))
if answer == 'y':
return matches
elif answer == 'n':
return _prompt()
return _prompt()
I've been stuck on it all day because as I see it, the hostfunc tries to return prompt which sees it has no answer, and asks you to "Pick from...", which calls _prompt with the answer. _prompt sees it has an answer, gets the matches, and since len(matches) == 1
, it should ask you if "Is dogs OK?" then immediately return ['dogs']
when you say 'y'.
I also tried doing x = _prompt()
and then returning x instead. Why does this thing ask you to confirm twice?
Thank you
Upvotes: 0
Views: 54
Reputation: 20076
It seems your if answer == None
is not returning after calling _prompt
again, so you'll iterate with the correct answer
variable twice
so you'll call _prompt()
with answer=None
, which will prompt input and then call _prompt(answer)
, which will find the correct matches. but once that _prompt(answer)
call is finished, it will resume with the callee which now has answer set accordingly, making it technically run the final answer twice.
changing _prompt(answer)
to return _prompt(answer)
should fix this
Upvotes: 1