RJB RJB - 1 year ago 65
Python Question

Program output is not as expected - Searching for int values in a list of tuples

I am coding a function which takes a search enquiry for a list of tuples (Employee records) and then outputs based on the enquiry parameters. For example, in my problem I am searching a salary range with a minimum of 30000 and a maximum of 100000, I would expect this to output the names of staff members within this salary range, however it instead give the output of no results found, which is incorrect as there are many staff members within this salary range.

As a comparison, a minimum of 0 and a maximum of 100000 would output all of the records which is correct, however when putting the minimum above 30000 it always outputs no result found which is infact correct as there are many staff salaries in the list of tuples which are over 30000.

Below is the part of the code which I believe is causing the problem (Not posting the whole code as this is a coursework project and I dont want to encourage plagiarism on my work):

elif (option == "salaries"):
editTup = sorted(tuplist, key=itemgetter(4,3))

print ("\nEnter the salary range of the employees you which to find\n")

salMin = int(input("Please enter minimum salary: "))
print (" ")
salMax = int(input("Please enter maximum salary: "))
x = 0
querFound = False
if salMin> salMax:
print ("\nThe minimum value cannot be higher than the maximum value\n")

while lenList>x :
t = editTup[x]
lst = list(t)
lst [2] = int (lst [2])
t = tuple(lst)

if t[2] > salMin and t[2] < salMax :
print("\n {:9}, {:9} \n".format(editTup[x][4], editTup[x][3]))
querFound = True

elif querFound == False and x == 0:
print ("\nNo results were found\n")
x = x + 1

The list of tuples supplied has been sorted by their names. The list of tuples looks a little like this:

(['12341','History','21000','Michael','Benson'],['12333','English','55000','Noel','Simms'] e.t.c

Note all values are strings, hence why I have converted the salary part to a integer so that it can be compared easier to another integer. Also the list of tuples is stored under the variable tuplist which you see in the beginning is further stored in editTup - the sorted version of the list.

The program output:

Enter the salary range of the employees you which to find

Please enter minimum salary: 30000

Please enter maximum salary: 100000

No results were found

This should actually output results as there are employees within this salary range, but it is apparent that something is wrong in the code, and I cannot see what this is!

I hope someone can help, this has been bothering me for a while and I really cant seem to find the solution to this!

Answer Source

I think your program exits after the first tuple is processed. Is the first tuple outside of the salary range? If so, it skips the if statement and goes to the elif. querFound is still false and x is still 0, so it prints and exits.

My suggestion is to replace your entire while loop with this for loop:

for t in editTup:
    sal = int(t[2])
    if sal > salMin and sal < salMax:
        print(t[4] + " " + t[3])
        querFound = True
if not querFound:
    print('No results found')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download