简体   繁体   中英

Why is code 1 working properly, while code 2 isn't ? (Python)

Im trying this excercise on Codebat:

Given a number n, return True if n is in the range 1..10, inclusive. Unless outside_mode is True, in which case return True if the number is less or equal to 1, or greater or equal to 10.

Code 1 :

def in1to10(n, outside_mode):
  if not outside_mode:
      return  n in range(1,11)
  return n <= 1 or n >= 10 

Code 2:

def in1to10(n, outside_mode):
  if outside_mode and  n <= 1 and n >= 10:
    return True
  elif n >= 1 and n <= 10:
    return True
  else:
    return False

Can somebody explain this to me, because i think these codes are quite similiar.

this line is wrong :

if outside_mode and  n <= 1 and n >= 10:

it is not possible that n is at the same time lower(or equal) 1 and greater(or equal) 10 it should be:

def in1to10(n, outside_mode):
  if outside_mode and  n <= 1 or n >= 10:
    return True
  elif n >= 1 and n <= 10:
    return True
  else:
    return False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM