简体   繁体   English

最小和最大查询无法正常运行(Python 3.5)

[英]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. 一切似乎都很好,直到我意识到当输入值“ 100000”作为最大值时,查询将不会输出任何内容。 Completely baffled I tried entering in '999999' which then worked and all records were print. 完全感到困惑,我尝试输入“ 999999”,该密码随后开始工作,并且所有记录均已打印。 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!): 以下是该特定部分的代码,输出的最大值为999999(我不希望粘贴整个程序,因为这是针对课程项目的,并且我想防止同一课程中的任何人潜在地复制我的作品,如果这使我的问题不清楚!):

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! 所有记录中的最高工资是55000,因此为什么最低0和最高100000无效,而最高999999无效,这是为什么呢!

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! 这可能看起来还不清楚,但是就像我在上面说的那样,我不想让班上的任何人to窃,并且我的工作也不会因此而无效! So I have tried to ask this without posting all my code on here! 因此,我试图在不将我的所有代码都发布在这里的情况下问这个问题!

When you read in from standard input in Python, no matter what input you get, you receive the input as a string. 当您从Python中的标准输入中读取内容时,无论您得到什么输入,您都会以字符串形式接收输入。 That means that your comparison function is resulting to: 这意味着您的比较函数将导致:

if tuplist[x][2] > "0" and tuplist[x][2] < "999999" :

Can you see what the problem is now? 您知道现在出了什么问题吗? Because it's a homework assignment, I don't want to give you the answer straight away. 因为这是一项家庭作业,所以我不想立即给您答案。

Given your use of the print function (instead of the Python 2 print statement), it looks like you're writing Python 3 code. 鉴于您使用了print函数(而不是Python 2 print语句),您似乎正在编写Python 3代码。 In Python 3, input returns a str . 在Python 3中, input返回str I'm guessing your data is also storing the salaries as str (otherwise the comparison would raise a TypeError ). 我猜您的数据也将薪水存储为str (否则比较将引发TypeError )。 You need to convert both stored values and the result of input to int so it performs numerical comparisons, not ASCIIbetical comparisons. 您需要将存储的值和input结果都转换为int以便它执行数字比较,而不是ASCIIbetical比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM