Determine Whether Integer Is Between Two Other Integers?

前端 未结 11 861
猫巷女王i
猫巷女王i 2020-11-22 15:07

How do I determine whether a given integer is between two other integers (e.g. greater than/equal to 10000 and less than/equal to 30000)?

I\

相关标签:
11条回答
  • 2020-11-22 15:24
    if 10000 <= number <= 30000:
        pass
    
    0 讨论(0)
  • 2020-11-22 15:25

    The condition should be,

    if number == 10000 and number <= 30000:
         print("5% tax payable")
    

    reason for using number == 10000 is that if number's value is 50000 and if we use number >= 10000 the condition will pass, which is not what you want.

    0 讨论(0)
  • 2020-11-22 15:27
    >>> r = range(1, 4)
    >>> 1 in r
    True
    >>> 2 in r
    True
    >>> 3 in r
    True
    >>> 4 in r
    False
    >>> 5 in r
    False
    >>> 0 in r
    False
    
    0 讨论(0)
  • 2020-11-22 15:37

    Your operator is incorrect. Should be if number >= 10000 and number <= 30000:. Additionally, Python has a shorthand for this sort of thing, if 10000 <= number <= 30000:.

    0 讨论(0)
  • 2020-11-22 15:37

    Define the range between the numbers:

    r = range(1,10)
    

    Then use it:

    if num in r:
        print("All right!")
    
    0 讨论(0)
  • 2020-11-22 15:38
    if number >= 10000 and number <= 30000:
        print ("you have to pay 5% taxes")
    
    0 讨论(0)
提交回复
热议问题