-2

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)
rakib neo
  • 1
  • 1
  • It would be helpful if you provide a sample input and an expected output. – Park Feb 04 '22 at 14:17
  • Does this answer your question? [Fastest way to check if a value exists in a list](https://stackoverflow.com/questions/7571635/fastest-way-to-check-if-a-value-exists-in-a-list) – Tomerikoo Feb 05 '22 at 08:03

5 Answers5

0

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)
Park
  • 2,446
  • 1
  • 16
  • 25
0

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)
Muhammad Mohsin Khan
  • 1,444
  • 7
  • 16
  • 23
0

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)
Anshumaan Mishra
  • 1,349
  • 1
  • 4
  • 19
0
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)
zeeshan12396
  • 382
  • 1
  • 8
0

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 ==

PsyQuake
  • 45
  • 8