Вот что я имею в виду, допустим, я работаю с проверкой, какому оператору телефонной связи принадлежит определенный номер телефона.

tel = input("Enter telephone number: ")

# Say I want to check if 077, #0782, 0783, 070 are in the user input
# Say the telephone numbers [0782123123, 078312312, 07712312] 
# which belong to Operator1 are entered by the user 
# I want be able to sort out the user's input 

if 0783, 077, 070,0782 are in tel: 
  Then print ("This number belongs to Operator 1")

and if 0755, 0723 are in tel:
   Then print ("This number belongs to Operator 2")
-1
Black Elephant 25 Май 2021 в 10:43

2 ответа

Лучший ответ

Вы можете использовать встроенную функцию any:

tel = input("Enter telephone number: ")

if any(num in tel for num in ('0783', '077', '070','0782')):
  print ("This number belongs to Operator 1")

if any(num in tel for num in ('0755', '0723')):
   print ("This number belongs to Operator 2")
0
big_bad_bison 25 Май 2021 в 10:56
tel = input("Enter telephone number: ")
check1 = ["0783", "077", "070", "0782"]
check2 = ["0755", "0723"]
if any(x in tel for x in check1):
    print("This number belongs to Operator 1")
elif any(x in tel for x in check2):
    print("This number belongs to Operator 2")
else:
    print("This number neither belongs to Operator 1 nor to 
                operator 2")

Надеюсь, это сработает.

0
Anushka Patel 26 Май 2021 в 14:38