How do you get around this error while using .pop
? I get that when it tries to return a number but there isn't one an error is raised but how do you get around it so the program keeps running?
def remove_element(self,integer):
self.integer = integer
self.members.pop()
Just check if self.members
is not empty:
if self.members:
self.members.pop()
or, catch KeyError
via try/except
:
try:
self.members.pop()
except KeyError:
# do smth
You can use try/except
to catch the KeyError
raised by an_empty_set.pop()
, or check the set first to make sure it's not empty:
if s:
value = s.pop()
else:
# whatever you want to do if the set is empty
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With