简体   繁体   English

为什么代码1不能正常工作,而代码2却不能正常工作? (蟒蛇)

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

Im trying this excercise on Codebat: 我在Codebat上尝试这种锻炼:

Given a number n, return True if n is in the range 1..10, inclusive. 给定数字n,如果n在1..10(含)范围内,则返回True。 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. 除非outside_mode为True,否则如果数字小于或等于1,或者大于或等于10,则返回True。

Code 1 : 代码1:

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

Code 2: 代码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: n不可能同时小于(或等于)1和大于(或等于)10,它应该是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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