ThomasL refactored code to take advantage of else: clause in for-loop (no need for sentinel):
For built-in help on for-loop (including else clause):
EDIT:
One can approach this problem with another angle: get all matching pairs in list and use conditional to print appropriate information:
>>> name = 'lizzy' >>> for wife, husband in husbands: ... if wife == name: ... print(f"{wife.title()}'s husband is {husband.title()}") ... break ... else: # no-break ... print(f"{name.title()} might be single as she is not in the list") ... Lizzy's husband is DarcyThere is of course question of uniqueness. This code returns first match but if there are several wives with same name what is expected result?
For built-in help on for-loop (including else clause):
>>> help('for')
EDIT:
One can approach this problem with another angle: get all matching pairs in list and use conditional to print appropriate information:
>>> matches = [f"{wife.title()}'s husband is {husband.title()}" for wife, husband in husbands if wife == name] >>> if matches: ... print(*matches, sep='\n') ... else: ... print(f"{name.title()} might be single as she is not in the list") ... Lizzy's husband is Darcy
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.