0

I am aware that you can call a Module in Python within itself, but can you also cancel it within itself? This code is supposed to only display that the name was found, one time. It continues to print until I interrupt the program with and get KeyboardInterrupt from the IDLE. Can I have some help on what I should put in or change? P.S.: I have Python 3.4.2

from array import *
s = ["Turner", "Philips", "Stevenson", "Jones", "Gonzalez",
"Whitaker", "Bruner", "Webster", "Foster", "Anderson",
"Klein", "Connors", "Rivers", "Wilson", "Duncan"]
g = [94, 82, 87, 78, 65,
90, 85, 97, 70, 100,
57, 88, 73, 92, 84]

s.sort()
for i in range(15):
    idx = int(i)

    studentList = s[idx]
    gradeList = g[idx]

    print(studentList, gradeList)

print("")
print("Student five: ", s[4], g[4])
print("Student ten: ", s[9], g[9])
print("Student fifteen: ", s[14], g[14])

gradeAverage = (g[0] + g[1] + g[2] + g[3] + g[4] + g[5] + g[6] + g[7] + g[8]
              + g[9] + g[10] + g[11] + g[12] + g[13] + g[14]) / 15

print("")
print("The average of the list of the grades is: ", gradeAverage)
print("The median of the list of grades is: ", g[7])
print("")

def byRank():
    studentFound = False
    index = 0
    searchValue = input("Student to be found: ")
    while(studentFound == False) & (index <= len(s) - 1):
        if s[index] == searchValue:
            studentFound == True
            print("Name", searchValue, "was found at index", index)
        else:
            index = index + 1
            print("Name not found. Searching...")
    print("Name is not in the array.")

byRank()
martineau
  • 119,623
  • 25
  • 170
  • 301
  • You indentation seems to be out of sorts...never mind, you fixed it. – Daniel Timberlake Feb 18 '15 at 19:54
  • What do you mean *"cancel it"*?! – jonrsharpe Feb 18 '15 at 19:56
  • It's unclear what you're asking but the problem with it not exiting the `while` might be because the condition is written wrong. Try changing it to `while(studentFound == False) and (index <= len(s) - 1):` otherwise you're doing a bitwise `&` operation not a logical AND. You also need to change the assignment statement to `studentFound = True`, not `==`. – martineau Feb 18 '15 at 19:58
  • Sorry. I learned some Java last year and now I'm taking a course with Python involved and I did not know if that worked. I also used to have a tendency to put all of my works into classes. – Ghost_Programmer Feb 18 '15 at 20:38
  • 1
    It seems that what's meant by 'module' is 'function', and what's meant by 'cancel' is something like 'break' or 'return'. – Tom Hunt Feb 18 '15 at 20:40

3 Answers3

1

The correct way to implement your byRank() function is using the list.index() method made just for that purpose. Try something more like:

def byRank():
    searchValue = input("Student to be found: ")
    try:
        index = s.index(searchValue)
    except ValueError:
        print("Name is not in the array.")
    else:
        print("Name", searchValue, "was found at index", index)
Tom Hunt
  • 916
  • 7
  • 21
0

studentFound == True does not change the value of studentFound. Use a single = for assignment. Or just use break for a simple case like this.

You usually don't use while loops to iterate over sequences in Python - instead, something like this would be better if you don't want to use the built-in index method:

for index, candidate in enumerate(s):
    if candidate == searchValue:
        print("Name", searchValue, "was found at index", index)
        break

But one problem at a time.

Peter DeGlopper
  • 36,326
  • 7
  • 90
  • 83
  • I am fairly new to Python and still learning my way around some things so I thought I'd ask, what does the `for index, candidate in enumerate(s)` do? Thank you. – Ghost_Programmer Feb 18 '15 at 20:28
  • Do you know what `for candidate in s:` does? If not, the Python tutorial will show you: https://docs.python.org/2/tutorial/controlflow.html#for-statements . `enumerate(s)` is a built in function that returns an iterable that gives you the index as well as the value from an input iterable, eg `enumerate(['a', 'b', 'c'])` will yield `(0, 'a')`, then `(1, 'b')`, then `(2, 'c')`. – Peter DeGlopper Feb 18 '15 at 21:05
  • I think I know what `for candidate in s:` does, but I will check out the tutorial anyways (thank you). You said `enumerate()` is a built-in function; can I use this only in lists, only in arrays, or both? So `enumerate()` will show the index number of an element, along with the element itself? Thank you for taking the time to respond in the first place. Have a great day! With kind regards, Ghost_Programmer – Ghost_Programmer Feb 19 '15 at 20:19
  • `enumerate()` can be called on any iterable (http://stackoverflow.com/a/9884259/2337736 for further definitions) - both built in lists and the `array.array` class from the standard library included. And, yes, it will show the index and the element - it's the standard way to step through an iterable when you might need the index in Python, rather than incrementing an index value as you might in C or Java. – Peter DeGlopper Feb 19 '15 at 20:24
0

You have several errors in your code. The biggest error is your while loop test. Here are the fixes

def byRank():
    studentFound = False
    index = 0
    searchValue = raw_input("Student to be found: ")
    while not studentFound and index < len(s):
        if s[index] == searchValue:
            studentFound = True
            print("Name ", searchValue, " was found at index ", index)
        else:
            index += 1
            print("Name not found. Searching...")
    print("Name is not in the array.")

FYI -- Tom Hunt's answer is the better way to write the code if you're willing to throw away your work.

user590028
  • 11,364
  • 3
  • 40
  • 57
  • Thank you all for your help. I was just making myself some lunch but I will try these solutions in the order in which they were posted. Thank you all again! – Ghost_Programmer Feb 18 '15 at 20:11
  • I will try the other answers (the ones that do not have the green check-mark) and see if they work as well. Thank you all for your help and have a wonderful day! With kind regards, Ghost_Programmer – Ghost_Programmer Feb 18 '15 at 20:16
  • This is just something I am putting together for school, but I will most likely change up my coding style in the future. Thank you. – Ghost_Programmer Feb 18 '15 at 20:19