Python conditon

hello guys the methods are corrects ? :sos:

 def has_space(self):
    return self.limit > self.size
 def has_space(self):
    if self.limit > self.size
else:
    return True
def has_space(self):
    if self.limit > self.size
    return True

The first is correct as it is.

The third would also be correct if you edited it to have a colon at the end of the if... statement and indented the next line:

def has_space(self):
  if self.limit > self.size:
    return True
1 Like

and the second why we canโ€™t use else

You have no need for else there.

def has_space(self):
  if self.limit > self.size # <- missing colon here
    # <- missing code block here
  else # <- missing colon here
    return True # <- you're returning true if the limit is *not* bigger than the size?  That should return false.

If you must use if and else, you can do it this way:

def has_space(self):
  if self.limit > self.size:
    return True
  else:
    return False

but thatโ€™s exactly the same as this:

def has_space(self):
  return self.limit > self.size

so you may as well go with the latter.

1 Like