Shakeeb Shahid
Shakeeb Shahid

Reputation: 1

Whats wrong? The PyCharm doesnt give any output

    array1 = []
size = int(input("ENTER SIZE"))
print("ENTER VALUES")
for i in range(0,size):
    l = int(input())
    array1.append(l)
x = int(input("ENTER SEARCH ELEMENT"))
def linearsearch(array1, size, x):
    pos = -1
    i = 0
    while i < size:
        if(array1.index(i) == x):
            pos = i
            break
        else:
            continue
    if(pos!=-1):
         print(pos)
    else:
        print(pos)

I have used while loop to find the element position corresponding with the search element x But the pycharm console doesnt print anything after the input please help

Upvotes: 0

Views: 79

Answers (1)

Bluehorn
Bluehorn

Reputation: 3091

It is obvious from the code that the last output prompt will be

ENTER SEARCH ELEMENT

This is because you only define linearsearch but never call it. Try adding this line at the end:

linearsearch(array1, size, x)

The question has a number of issues IMO:

  • this has nothing to do with PyCharm (running it from command line gives the same result)
  • you should be more detailed in what the actual output is and what you would have expected

Actually there is output generated (the prompts for input), so the question asserts something that is not entirely correct.

BTW: Even if you add the call to linearsearch, the code still has some bugs:

  • array1.index(i) does not return the item at index i but does a linear search for an item with value i. You meant array1[i].
  • i is never incremented so the while loop will never terminate. Use i = i + 1 instead of continue.

Upvotes: 0

Related Questions