-1

Write a program to check a given number is present in list. If present, display the position of the element in a list. If the element is not present, add the element to the list.

 l=[1,2,4,3,6]
    x=int(input("enter a number:"))
    for i in l:
        if(i == x):
            print("number present at")
            print(l.index(x))
        else:
            l.append(x)

 
Dilshan
  • 120
  • 3
  • 9
  • 2
    Please define "not working". That could mean a lot of different things. If you're getting an error message then include that in your question (the full message including traceback). The current issues I see are incorrect indentation (possibly just from copy-pasting) and, more importantly, that you'll append x to the list every time the loop iterates. – Kemp Apr 01 '21 at 15:10
  • Modifying a list as you're iterating over it will usually produce weird behavior. Why are you doing this in a `for` loop at all rather than just doing `if x in l`? – Samwise Apr 01 '21 at 15:11
  • Oh and please do not use a lowercase `L` as a variable name. –  Apr 01 '21 at 15:11
  • WIthout a stacktrace it's not certian what's wrong, but you're probably getting strange results because you're [modifying your list while iterating over it](https://stackoverflow.com/questions/6260089/strange-result-when-removing-item-from-a-list-while-iterating-over-it) – Christian Dean Apr 01 '21 at 15:12

5 Answers5

2

You don't need a for loop to do this. A simple 'if x in y' statement will be enough.

l = [1,2,4,3,6]
x = int(input("enter a number:"))
if x in l:
    print(l.index(x))
else:
    l.append(x)
coderX
  • 29
  • 4
1

There are some problems with the indentation. You need to move all lines, except the first line of code, one tab left.

As for the code, one option could be to add a break statement, that stops the for-loop from running if the number is found. If the number isn't found, the else-condition will run. If you choose this implementation, the else-condition and its contents should be moved one more tab to the left, so it aligns with the for-loop.

l=[1,2,4,3,6]
x=int(input("enter a number:"))
for i in l:
    if(i == x):
        print("number present at")
        print(l.index(x))
        break
else:
    l.append(x)
mns
  • 26
  • 3
  • Why using `l.index()` while iterating the list? You'll look through it twice instead of direct printing current index as with `enumerate()`. – STerliakov Apr 01 '21 at 15:22
  • True, I tried to keep as much of the questioner's code as possible. A shorter version could be: try: print(f"number present at: {l.index(x)}") except: l.append(x) – mns Apr 01 '21 at 15:30
1

you dont have to print for each number in the list

numbers = [1, 2, 4, 3, 6]
input_number = int(input("enter a number: "))

if input_number in numbers:
    print(f"number already in list at {numbers.index(input_number)}")
else:
    numbers.append(input_number)
Bjorn
  • 103
  • 12
1

There is no need to iterate through the list, especially since you are using index() already:

l = [1,2,4,3,6]
x = int(input("enter a number:"))
if x in l:
    print("number present at")
    print(l.index(x))
else:
    l.append(x)
quamrana
  • 37,849
  • 12
  • 53
  • 71
0

Your code does not the thing you described. It appends element multiple times (ones for each element that is not equal to given). If you want to use li.index() method, the following will do the trick:

li = [1,2,4,6]
vals = [1,3]
for val in vals:
    if val in li:
        print(li.index(val))
    else:
        li.append(val)

If your value can be present in list multiple times, you can get all its occurrences positions with list comprehension instead of index(): print([i for i,e in enumerate(li) if e==v])

STerliakov
  • 4,983
  • 3
  • 15
  • 37