1

Lets see i have a condition :

a = int(b) >= 1230 and int(b) not in [1300, 1305, 1250]

Here,the list could have multiple values [1300,1303,1306,1307] etc. So i want to check:

if int(b) >= 1230 and int(b) = 130* and int(b) != 1250:
    do something
else:
    so something

How can i check for the numbers starting 130*?

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Soni
  • 49
  • 1
  • 3
  • 2
    Your question is not clear. Please edit it to add more details and what you have tried so far. – Ankit Jaiswal Jul 10 '18 at 05:32
  • `(int(b) >= 1230) and (int(b) != 1250) and (str(b)[:3] == 130)`? – jpp Jul 10 '18 at 05:32
  • One way is to convert the number to str then use `str.startswith('130')` – Chiheb Nexus Jul 10 '18 at 05:32
  • Possible duplicate of [Converting integer to string in Python?](https://stackoverflow.com/questions/961632/converting-integer-to-string-in-python) – Chiheb Nexus Jul 10 '18 at 05:34
  • 1
    `int(b) in range(1300,1310)` would detect if b is 1300-1309 ... you should convert `b` once before you you can avoid all those `int(b)` here and `int(b)` there ... if you have more then 4 or 5 values inside your list you want to compare b agains, use a `set(1300, 1305, 1250,1400,1500,1700,1999)` instead , its much faster that way. you could also test `intB // 10 == 130` to check if intB is 130* . – Patrick Artner Jul 10 '18 at 05:38
  • Do you mean int(b[:3])==130? But it cannot be 1250 of course. Condition int(b) != 1250 is meaningless here. – Marcus.Aurelianus Jul 10 '18 at 05:41
  • To be more clear, i have i am taking a value of 'b' from user and converting it to a int type. Then i am using the same value for my condition which is :a = int(b) >= 1230 and int(b) not in [1300, 1305] and int(b) != 1250.Here the list could be increased in future where i can have 1300,1301,1306,13006. so i want to freeze the check here by giving something called as 130*, where * can be any digit after 130.Is it possible to wild-card 13.0*? – Soni Jul 10 '18 at 05:43
  • What do you mean by int(b) not in [1300, 1305] and i can have 1300,1301,1306? [1300,1305] is a range or list? Bounded or not? – Marcus.Aurelianus Jul 10 '18 at 05:52

3 Answers3

3

How about

if int(b) >= 1230 and str(b).startswith('130') and int(b) != 1250:
    do something
else:
    do something
Sunitha
  • 11,777
  • 2
  • 20
  • 23
1

You can check if a number is inside a range() if you have a continious range to cover. Do not convert to int multiple times, store your int value: bAsInt = int(b) and use that.

If you want to check against specific single values, use a set() if you have 4 or more values - it is faster that a list-lookup:

even = {1300,1302,1304,1306,1308}

for number in range(1299,1311):
    # print(number," is 130* :", number//10 == 130 ) # works too, integer division
    print(number," is 130* :", number in range(1300,1310), 
          " and ", "even" if number in even else "odd")

Output:

1299  is 130* : False  and  odd
1300  is 130* : True  and  even
1301  is 130* : True  and  odd
1302  is 130* : True  and  even
1303  is 130* : True  and  odd
1304  is 130* : True  and  even
1305  is 130* : True  and  odd
1306  is 130* : True  and  even
1307  is 130* : True  and  odd
1308  is 130* : True  and  even
1309  is 130* : True  and  odd
1310  is 130* : False  and  odd

Take value from user and compare:

Could be solved like so:

def inputNumber():
    # modified from other answer, link see below       
    while True:
        try:
            number = int(input("Please enter number: "))
        except ValueError:
            print("Sorry, I didn't understand that.")
            continue
        else:
            return number

b = inputNumber()

even = {1300,1302,1304,1306,1308}

if b > 1230 and b not in even and b in range(1300,1310):
    #  r > 1230 is redundant if also b in range(1300,1310)
    print("You won the lottery")

It will print smth for 1301,1303,1305,1307,1309 (due to the in even) .


Other answer: Asking the user for input until they give a valid response

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
0

You can also use this

def start_with(nub, start, stop):
    return [x for x in range(start,stop) if str(x).startswith(str(nub))]

print(start_with(130, 1, 5000))

use of this function you can find any list start with any number

Om trivedi
  • 32
  • 3