Skip to content

Instantly share code, notes, and snippets.

@westscz
Last active March 23, 2020 20:34
Show Gist options
  • Save westscz/00faaf3392cf9a365b1133e3abe62f0f to your computer and use it in GitHub Desktop.
Save westscz/00faaf3392cf9a365b1133e3abe62f0f to your computer and use it in GitHub Desktop.
def print_fifth_element(iterable):
passed = False
try:
print(iterable[4])
passed = True
except IndexError:
iterable.append(iterable[-1])
except TypeError:
iterable = [iterable]
except KeyError as e:
iterable[e.args[0]] = e.args[0]
finally:
if passed:
return iterable[4]
return print_fifth_element(iterable)
print_fifth_element('Hipopotam')
print_fifth_element([1,2,3])
print_fifth_element(None)
print_fifth_element({})
"""
p
3
None
4
"""
def print_fifth_element(iterable):
passed = False
try:
print(iterable[4])
passed = True
except IndexError:
missing = 5-len(iterable)
iterable.extend([iterable[-1]]*missing)
except TypeError:
iterable = [iterable]*5
except KeyError as e:
iterable[e.args[0]] = e.args[0]
finally:
if not passed:
print(iterable[4])
print_fifth_element('Hipopotam')
print_fifth_element([1,2,3])
print_fifth_element(None)
print_fifth_element({})
"""
p
3
None
4
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment