0

Eg.

Mylist = [1,2,3,4,5]

User_ip = 3

#user enters a value that is in the python list and if not print- not in the list

I am trying it with for loop but logic not works

for i in Mylist:
    if i== User_ip:
        print ("value is in the list")
    else:
        print ("value not in the list")
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
Aakash
  • 1

3 Answers3

2

Why not just check :

if User_ip in Mylist:
    print ("value is in the list")
else:
    print ("value not in the list")
Charles Dupont
  • 995
  • 3
  • 9
1

You can check if a item is in the list with the in Operator:

Mylist = [1,2,3,4,5]

User_ip = 3

if User_ip in Mylist:
    print("value is in the list")
else:
    print("value not in the list")
Eitan Rosati
  • 543
  • 5
  • 18
0
# Input
Mylist = [1,2,3,4,5]
User_ip = 3

if User_ip in MyList:
    print ("value is in the list")
else:
    print ("value not in the list") 
  • 2
    While code-only answers might answer the question, you could significantly improve the quality of your answer by providing context for your code, a reason for why this code works, and some references to documentation for further reading. From [answer]: _"Brevity is acceptable, but fuller explanations are better."_ – Pranav Hosangadi May 17 '21 at 21:44
  • 1
    Ok i got it man...! Thank you what for loop did is list iterated in for loop and if else replaced the print statement with numbers – Aakash May 18 '21 at 18:53