RJB RJB - 1 year ago 91
Python Question

Minimum and Maximum query not working properly (Python 3.5)

I wonder if you can help because I've been looking at this for a good half hour and I'm completely baffled, I think I must be missing something so I hope you can shed some light on this.

In this area of my program I am coding a query which will search a list of tuples for the salary of the person. Each tuple in the list is a separate record of a persons details, hence I have used two indexes; one for the record which is looped over, and one for the salary of the employee. What I am aiming for is for the program to ask you a minimum and maximum salary and for the program to print the names of the employees who are in that salary range.

It all seemed to work fine, until I realised that when entering in the value '100000' as a maximum value the query would output nothing. Completely baffled I tried entering in '999999' which then worked and all records were print. The only thing that I can think of is that the program is ignoring the extra digit, which I could not figure out why this would be?!

Below is my code for that specific section and output for a maximum value of 999999 (I would prefer not to paste the whole program as this is for a coursework project and I want to prevent anyone on the same course potentially copying my work, sorry if this makes my question unclear!):

The maximum salary out of all the records is 55000, hence why it doesnt make sense that a minimum of 0 and maximum of 100000 does not work, but a maximum of 999999 does!

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

salMin = input("Please enter minimum salary: ")
print (" ")
salMax = input("Please enter maximum salary: ")
x = 0

while lenList>x :
if tuplist[x][2] > salMin and tuplist[x][2] < salMax :
print ("\n", tuplist[x][3], tuplist[x][4], "\n")
x = x + 1

When ran and max value is 999999:

Please enter minimum salary: 0

Please enter maximum salary: 999999

Bob Smith

Barry Potter

Gus Johannson

David Cameron

Louis Gibbs

Sarah Python

Tony Thompson

Lemar Davies

Ken Barnes

Jennifer Johnson

Please enter a search query (departments or salaries)
or type exit to quit (note options are case sensitive):

When ran with a max value of 100000

Please enter minimum salary: 0

Please enter maximum salary: 100000
Please enter a search query (departments or salaries)
or type exit to quit (note options are case sensitive):

If any more information is need to help, please ask! This probably seems unclear but like I said above, I dont want anyone from the class to plagiarise and my work to be void because of that! So I have tried to ask this without posting all my code on here!

Answer Source

Given your use of the print function (instead of the Python 2 print statement), it looks like you're writing Python 3 code. In Python 3, input returns a str. I'm guessing your data is also storing the salaries as str (otherwise the comparison would raise a TypeError). You need to convert both stored values and the result of input to int so it performs numerical comparisons, not ASCIIbetical comparisons.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download