8 Replies - 270 Views - Last Post: 17 April 2013 - 05:41 PM Rate Topic: -----

#1 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • Joined: 07-October 12

Error through looping

Posted 17 April 2013 - 04:33 AM

I understand the algorithm on how the computer is processing through my program.

Here is a piece of code that have been rewritten:

def nameSearch(list_names, name):
    len_student = len(list_names)
    for i in range (0,9):
        if  (list_names[i] == name):
            print(i)
            break
        elif (list_names[i] != name):
            return -1

names = ""
names_list = ["Bob", "John", "Jack", "Alice", "Jane"]

length_name = len(names_list)

names = input("Which student do you want to find: ")
nameSearch(names_list, names)


So while it is going through the loop, the computer will do one of the argument:

First one, if the name is in the list it will print the position number that is in the array. Although if the name the user inputted is not on the list, than it will print -1.

The problem with this code is only checks the first one, as I attempted to use break and return function, as this is giving me not the desired answer.

I want to make sure that it loops through until it finds the user, and than breaks. Instead of looping and breaking in the first element of the array.

Is This A Good Question/Topic? 0
  • +

Replies To: Error through looping

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Error through looping

Posted 17 April 2013 - 04:57 AM

sorry - didn't understand what you were asking the first time.

okay you won't know if the name was not in the list until you have finished looping through the whole list. That makes sense doesn't it?

So set a flag to false before you start looping.
If you find a name set the flag to true before you break out of the loop.

Outside the scope of the loop test the flag.
If it is true then the name was in the list.
If it is false then... <you can answer that can't you?>

This post has been edited by janotte: 17 April 2013 - 05:02 AM

Was This Post Helpful? 0
  • +
  • -

#3 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • Joined: 07-October 12

Re: Error through looping

Posted 17 April 2013 - 06:36 AM

I don't really get what you were trying to say. My simple question was:

How do I fix the loop, so that it will loop through all the names in the element. Because my loop will check only first element and than break.


PS: Note the loop is out of range, I was testing with 9 elements, I made it to 5, please exclude that when you helping me. Thanks in advance.
Was This Post Helpful? 0
  • +
  • -

#4 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Error through looping

Posted 17 April 2013 - 06:51 AM

This would fix your code:
def nameSearch(list_names, name):
    for i,guy in enumerate(list_names):
        if  guy == name:
            return i
    return -1

names_list = ["Bob", "John", "Jack", "Alice", "Jane"]
names = input("Which student do you want to find: ")
print(nameSearch(names_list, names))

However I suggest something more like this:
def nameSearch(list_names, name):
    try:
        return list_names.index(name)
    except ValueError:
        return -1

names_list = ["Bob", "John", "Jack", "Alice", "Jane"]
names = input("Which student do you want to find: ")
print(nameSearch(names_list, names))

You also might want to consider making it case insensitive.

-Mek
Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,747
  • Joined: 19-March 11

Re: Error through looping

Posted 17 April 2013 - 06:53 AM

def nameSearch(list_names, name):
    len_student = len(list_names)  # okay
    for i in range (0,9):          # wait, what? 




Why are you using a hard-coded range, instead of the length of the list?

        if  (list_names[i] == name):   # if this item is the one we're looking for
            print(i)                   # print it and 
            break                      # exit the loop
        elif (list_names[i] != name):  # otherwise
            return -1                  # just exit the loop and the function




Have we understood the problem now?
Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Error through looping

Posted 17 April 2013 - 07:06 AM

View Postchan 06, on 18 April 2013 - 12:36 AM, said:

I don't really get what you were trying to say.


Do you understand the term 'flag'?

I am not sure how much shared language we have here.

Here's the very simple algorithm I am suggesting.

foundName = false
positionInLoop = 0
loop through the name list
    if current list item == name then
        foundName = true
        positionInLoop = loop iterator
        break out of loop

if foundName == true then
    print PositionInLoop
else
    print -1



This is a terrible way to find a match in a list in Python but I am assuming you are using the looping idea because you have been told you have to.

If you just want to find an item in a list then there are far better ways to do this.
Have a read here
http://docs.python.o...structures.html

This post has been edited by janotte: 17 April 2013 - 07:23 AM

Was This Post Helpful? 0
  • +
  • -

#7 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • Joined: 07-October 12

Re: Error through looping

Posted 17 April 2013 - 10:21 AM

@Mekire,
Could you please explain what is going on with the program?
def nameSearch(list_names, name):
    for i,guy in enumerate(list_names):
        if  guy == name:
            return i
    return -1



View PostMekire, on 17 April 2013 - 06:51 AM, said:

This would fix your code:
def nameSearch(list_names, name):
    for i,guy in enumerate(list_names):
        if  guy == name:
            return i
    return -1

names_list = ["Bob", "John", "Jack", "Alice", "Jane"]
names = input("Which student do you want to find: ")
print(nameSearch(names_list, names))

However I suggest something more like this:
def nameSearch(list_names, name):
    try:
        return list_names.index(name)
    except ValueError:
        return -1

names_list = ["Bob", "John", "Jack", "Alice", "Jane"]
names = input("Which student do you want to find: ")
print(nameSearch(names_list, names))

You also might want to consider making it case insensitive.

-Mek

Was This Post Helpful? 0
  • +
  • -

#8 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Error through looping

Posted 17 April 2013 - 03:46 PM

We loop through the entire list. If at any point we find the item we are looking for, the function returns the index. If we loop through the entire list without finding it, we return negative one. enumerate is the pythonic way to loop through an iterable if you are going to also need the index; every time it loops, i is incremented by one, and guy is assigned the next item in the list.

Only after the for loop has looked (and rejected) every item, do we proceed to return negative one. Your function on the other hand, looked at the first item; checked if it was correct; and if it wasn't it immediately returned negative one. It never proceeded to the second item because under both conditions you stopped the loop. If a function at any point hits a return statement, then that function ends. I believe that is (one of) the points of confusion here.

-Mek

This post has been edited by Mekire: 17 April 2013 - 03:47 PM

Was This Post Helpful? 2
  • +
  • -

#9 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • Joined: 07-October 12

Re: Error through looping

Posted 17 April 2013 - 05:41 PM

Alright, I see what you means. Yes that's right, I was confused one my loop wouldn't iterate through all the lists before checking one of the arguments above, once again thanks for help.

View PostMekire, on 17 April 2013 - 03:46 PM, said:

We loop through the entire list. If at any point we find the item we are looking for, the function returns the index. If we loop through the entire list without finding it, we return negative one. enumerate is the pythonic way to loop through an iterable if you are going to also need the index; every time it loops, i is incremented by one, and guy is assigned the next item in the list.

Only after the for loop has looked (and rejected) every item, do we proceed to return negative one. Your function on the other hand, looked at the first item; checked if it was correct; and if it wasn't it immediately returned negative one. It never proceeded to the second item because under both conditions you stopped the loop. If a function at any point hits a return statement, then that function ends. I believe that is (one of) the points of confusion here.

-Mek

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1