i want to search variable using input .
found = False
number = [1,2,3,4,5,6,7,8,9,]
for value in number :
if value == 3 :
found = True
print(found)
i want to search variable using input .
found = False
number = [1,2,3,4,5,6,7,8,9,]
for value in number :
if value == 3 :
found = True
print(found)
If you want to get a value using input()
and check if the value is in a list, you can do it like this:
value = int(input())
found = False
number = [1,2,3,4,5,6,7,8,9,]
result = value in number
print(result)
You can do something like:
value = int(input("Insert a value: "))
found = False
number = [1,2,3,4,5,6,7,8,9]
for i in number:
if i == value :
found = True
break
print(found)
The method used by @SangkeunPark is more efficient, but it can be done with your program with a simple change:
found = False
a = int(input())
number = [1,2,3,4,5,6,7,8,9,]
for value in number :
if value == a :
found = True
print(found)
n=int(input('Enter a value:'))
number = [1,2,3,4,5,6,7,8,9]
if n in number:
print(True)
else:
print(False)
Alright so first we are going to make the variables (just like you had in your code)
found = False
number = [1,2,3,4,5,6,7,8,9]
now we will add an input command and store the respective value in variable called 'value'
value = int(input("Enter your number: "))
Now to check if the user's value is in our list or not, we are going to iterate through the list and in every iteration we will check if the value user entered appears to be in the list, if it does we will just set the 'found' variable to True
for values in number:
if values == value :
found = True
break
if found:
print("number found")
else:
print("number not found")
Now here you can use both "in" and "==" but since we are just checking the numbers in this particular piece of code, == is fine!
check out this for more info about in and ==