-1
arr = input("enter array:")
print(arr)
num = input("enter num:")
print(num)
    
for n in arr:
  if num == n:
       print("yes")
  else:
       print("no")

Output comes as :

no
no
no
yes
no
no
no

but my expectation is only yes or no when the number is present is list.

Am I missing something here?

Prune
  • 76,765
  • 14
  • 60
  • 81
  • `input` inputs a string, not an array. – DeepSpace Jan 09 '21 at 18:52
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). "Show me how to solve this coding problem?" is off-topic for Stack Overflow. You have to make an honest attempt at the solution, and then ask a *specific* question about your implementation. – Prune Jan 09 '21 at 19:18
  • Stack Overflow is not intended to replace existing tutorials and documentation. Reading in a line of data as a list is covered in many SO postings. Finding an element in a list is covered in any tutorial on lists. We expect you to use these resources before posting a question. – Prune Jan 09 '21 at 19:18

6 Answers6

1

There are some problems with your code. When you write input("enter array") , you get a string, not a list. So lets say arr = "10 20 30 40" Then you say - for n in arr: which, when perfmorning on a string means: for each character in the string, do : ... So what you get is n="1", then n="0", and so on.. which is not what you mean.

Now to fix your problem, if you only want to get list of string representing numbers from the user, and check if some other string representing a number, you can do it without a loop with just: if num in arr: if arr = "10 20 30 40" and num = "20" you will get True and print yes, and if num = "21" you will get False and print no..

But if you really need to use that num and arr as numbers, you need to parse the input into numbers Good luck my friend!

Gal Birka
  • 581
  • 6
  • 16
1

The input is a string, not an array. To turn it into an array, use this:

arr = [(item) for item in input("Enter the list items: ").split()]

Together, your code should look like this:

arr = [(item) for item in input("Enter the list items: ").split()] 
print(arr)

num = input("enter num: ")
print(num)
    
for n in arr:
    if num == n:
        print("yes")
    else:
        print("no")

NOTE: the input for the list should be in the format 3 6 8 1 8.

Alternativley, you could use if... in... to check. Try something like this:

arr = input("enter array:")
print(arr)
num = input("enter num:")
print(num)
    
if num in arr:
    print("yes")
else:
    print("no")

However, I wouldn't reccomend this because you don't use a list, you use a string, so it can get messy!

The Pilot Dude
  • 2,091
  • 2
  • 6
  • 24
1

you print yes or no for every number in the arry if you want just to know if the number is present you can do it by using in, try this

if num in arr: print("yes") else: print("no")
jonathan
  • 269
  • 1
  • 7
0

In input i suspect that you have entered array with length 7 and in for loop each and every item will iterate one by one and falling into if and else condition.

  1. If you want to Return after Finding that Number Put return in if condition after print statement.
  2. If you want to Match Multiple Number Use Elif or Switch

and You got answer this answer because, Every n number will falls into if condition if it matched and falls into else condition if n wont match.

0

Expect this might be something you are looking for:

nums = input("Enter numbers separated by comma: ")

your_num = int(input("Matching number: "))

nums = [int(x.strip()) for x in nums.split(",")]

if your_num in nums:
    print("Yes")
else:
    print("No")
Leemosh
  • 883
  • 6
  • 19
0

Firstly, you may use:

arr = eval(input())

to input a list instead of input() since it'll treat the input as a string.

Coming to the code now. By running that code, you are basically giving the following instructions:

  1. Go through each element in list one by one.
  2. Check whether it's equal to the desired value.
  3. If it is, print 'yes' and if it's not print 'no'

You see, you are making the decision to print out 'yes' or 'no' after checking every single element. I hope you realise the fault in your code now.

One of the ways you can accomplish your task is by using a variable:

found = False
for i in arr:
    if i == num:
        found = True
if found == True:
    print('yes')
else:
    print('no')

You can also use the 'in' statement which is relatively easier (but provides less algorithmic insight):

if num in arr:
    print('yes')
else:
    print('no')
OneCoolBoi
  • 86
  • 5